Skip to content

Commit

Permalink
Bump dependencies in /firebase/functions (#102)
Browse files Browse the repository at this point in the history
Updates `path-to-regexp` from 0.1.7 to 0.1.10
Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.2.8 to 5.4.6.
Updates `serve-static` from 1.15.0 to 1.16.2
Updates `send` from 0.18.0 to 0.19.0
Updates `body-parser` from 1.20.2 to 1.20.3
Bumps [rollup](https://github.com/rollup/rollup) from 4.14.0 to 4.22.4.
Bumps [fast-xml-parser](https://github.com/NaturalIntelligence/fast-xml-parser) from 4.3.6 to 4.5.0.
Updates `cookie` from 0.6.0 to 0.7.1
Updates `express` from 4.19.2 to 4.21.1
  • Loading branch information
daohoangson authored Oct 31, 2024
1 parent ea7785c commit e795bb1
Show file tree
Hide file tree
Showing 9 changed files with 574 additions and 538 deletions.
2 changes: 1 addition & 1 deletion firebase/functions/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports = {
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["tsconfig.json", "tsconfig.dev.json"],
sourceType: "module",
},
ignorePatterns: [
Expand All @@ -28,5 +27,6 @@ module.exports = {
quotes: ["error", "double"],
"import/no-unresolved": 0,
indent: ["error", 2],
"require-jsdoc": 0,
},
};
1,030 changes: 541 additions & 489 deletions firebase/functions/package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions firebase/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
"firebase-functions": "^4.9.0",
"formdata-node": "^4.4.1",
"node-fetch-commonjs": "^3.3.1",
"puppeteer": "^21.3.4"
"puppeteer": "^21.3.4",
"valibot": "^0.42.1"
},
"devDependencies": {
"@types/jsdom": "^21.1.3",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"@typescript-eslint/eslint-plugin": "^8.12.2",
"@typescript-eslint/parser": "^8.12.2",
"@vitest/coverage-v8": "^0.34.5",
"eslint": "^8.9.0",
"eslint-config-google": "^0.14.0",
Expand Down
38 changes: 12 additions & 26 deletions firebase/functions/src/cron.spec.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,54 @@
import { getDateFromRepo } from "./helpers/github";
import { getDateFromSource } from "./helpers/puppeteer";
import { send } from "./helpers/telegram";
import { send as sendTelegram } from "./helpers/telegram";
import { compareDateValuesBetweenSourceAndRepo } from "./cron";
import { describe, expect, it, vi } from "vitest";

const mocks = vi.hoisted(() => {
return {
getDateFromRepo: vi.fn() satisfies typeof getDateFromRepo,
getDateFromSource: vi.fn() satisfies typeof getDateFromSource,
sendTelegram: vi.fn() satisfies typeof send,
};
});

vi.mock("./helpers/github", () => ({
getDateFromRepo: mocks.getDateFromRepo,
}));
vi.mock("./helpers/puppeteer", () => ({
getDateFromSource: mocks.getDateFromSource,
}));
vi.mock("./helpers/telegram", () => ({
send: mocks.sendTelegram,
}));
vi.mock("./helpers/github");
vi.mock("./helpers/puppeteer");
vi.mock("./helpers/telegram");

const mockedConsoleLog = vi
.spyOn(console, "log")
.mockImplementation(() => undefined);

describe("compareDateValuesBetweenSourceAndRepo", () => {
it("should send Telegram with errors", async () => {
mocks.getDateFromSource.mockRejectedValueOnce(new Error("source"));
mocks.getDateFromRepo.mockRejectedValueOnce(new Error("repo"));
vi.mocked(getDateFromSource).mockRejectedValueOnce(new Error("source"));
vi.mocked(getDateFromRepo).mockRejectedValueOnce(new Error("repo"));
await compareDateValuesBetweenSourceAndRepo();

expect(mocks.sendTelegram).toHaveBeenCalledWith(
expect(vi.mocked(sendTelegram)).toHaveBeenCalledWith(
"❌❌❌\n[getDateFromSource] Error: source\n[getDateFromRepo] Error: repo",
expect.anything()
);
});

it("should send Telegram with a warning if the dates are different", async () => {
mocks.getDateFromSource.mockResolvedValueOnce({
vi.mocked(getDateFromSource).mockResolvedValueOnce({
date: "2023-01-01",
error: undefined,
png: Buffer.from(""),
});
mocks.getDateFromRepo.mockResolvedValueOnce({
vi.mocked(getDateFromRepo).mockResolvedValueOnce({
date: "2023-01-02",
error: undefined,
});
await compareDateValuesBetweenSourceAndRepo();

expect(mocks.sendTelegram).toHaveBeenCalledWith(
expect(vi.mocked(sendTelegram)).toHaveBeenCalledWith(
"2023-01-01 ❌ 2023-01-02",
expect.anything()
);
});

it("should log a message if the dates are the same", async () => {
mocks.getDateFromSource.mockResolvedValueOnce({
vi.mocked(getDateFromSource).mockResolvedValueOnce({
date: "2023-01-01",
error: undefined,
png: Buffer.from(""),
});
mocks.getDateFromRepo.mockResolvedValueOnce({
vi.mocked(getDateFromRepo).mockResolvedValueOnce({
date: "2023-01-01",
error: undefined,
});
Expand Down
2 changes: 1 addition & 1 deletion firebase/functions/src/helpers/puppeteer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("getDateInBrowserContext", () => {
});

it("should return undefined", () => {
const dom = new JSDOM(`<p>Foo</p>`);
const dom = new JSDOM("<p>Foo</p>");
vi.stubGlobal("document", dom.window.document);

const date = getDateInBrowserContext();
Expand Down
19 changes: 7 additions & 12 deletions firebase/functions/src/helpers/telegram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,28 @@ import { describe, expect, it, vi } from "vitest";
import { send } from "./telegram";
import { config } from "firebase-functions/v1";

const mocks = vi.hoisted(() => {
return {
config: vi.fn() satisfies typeof config,
};
vi.mock("firebase-functions", async (importActual) => {
const actual = await importActual<typeof import("firebase-functions")>();
return { ...actual, config: vi.fn() };
});

vi.mock("firebase-functions", () => ({
config: mocks.config,
}));

describe("telegram/send", () => {
const token = process.env.TELEGRAM_TOKEN ?? "";
const testIfHasToken = token.length > 0 ? it : it.skip;
testIfHasToken("should return true", async () => {
mocks.config.mockReturnValueOnce({
vi.mocked(config).mockReturnValueOnce({
telegram: {
token,
chat_id: "552046506", // bot chat with @daohoangson
},
});

const actual = await send("Hello");
const actual = await send(`[${new Date().toISOString()}] ${__filename}`);
expect(actual).toBeTruthy();
});

it("should return false with invalid token", async () => {
mocks.config.mockReturnValueOnce({
vi.mocked(config).mockReturnValueOnce({
telegram: { token: "foo", chat_id: "bar" },
});

Expand All @@ -37,7 +32,7 @@ describe("telegram/send", () => {
});

it("should throw with bad config", async () => {
mocks.config.mockReturnValueOnce({});
vi.mocked(config).mockReturnValueOnce({});
expect(send("Oops")).rejects.toThrow("Telegram config is incomplete!");
});
});
10 changes: 7 additions & 3 deletions firebase/functions/src/helpers/telegram.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as functions from "firebase-functions";
import { Blob, FormData } from "formdata-node";
import fetch from "node-fetch-commonjs";
import { boolean, object, safeParse } from "valibot";

const ResponseSchema = object({ ok: boolean() });

export async function send(text: string, options: { png?: Buffer } = {}) {
const config = functions.config() as {
Expand Down Expand Up @@ -30,14 +33,15 @@ export async function send(text: string, options: { png?: Buffer } = {}) {

const url = `https://api.telegram.org/bot${token}/${action}`;
const response = await fetch(url, { body: data, method: "POST" });
const json = (await response.json()) as any;
const ok = json?.ok;
if (typeof ok !== "boolean") {
const json = (await response.json()) as unknown;
const { output, success } = safeParse(ResponseSchema, json);
if (!success) {
throw new Error(
`Unexpected response from Telegram: ${JSON.stringify(json)})}`
);
}

const ok = output.ok;
if (!ok) {
console.log({ text, json });
}
Expand Down
3 changes: 0 additions & 3 deletions firebase/functions/tsconfig.dev.json

This file was deleted.

1 change: 1 addition & 0 deletions firebase/functions/vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export default defineConfig({
coverage: {
reporter: ["lcov", "text-summary"],
},
include: ["src/**/*.spec.ts"],
},
});

0 comments on commit e795bb1

Please sign in to comment.