From 8bc328e8b422b57dfd0e53ca96770946cf509747 Mon Sep 17 00:00:00 2001 From: Okiki Ojo Date: Thu, 2 Apr 2026 21:55:59 -0400 Subject: [PATCH] test: update logging tests to verify log schema recreation and add new deploy config tests Signed-off-by: Okiki Ojo --- tests/integration/synology.test.ts | 10 +++++-- tests/integration/update.test.ts | 9 ++++-- tests/prepare-deploy-config.test.ts | 44 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 tests/prepare-deploy-config.test.ts diff --git a/tests/integration/synology.test.ts b/tests/integration/synology.test.ts index c76fd59..2067060 100644 --- a/tests/integration/synology.test.ts +++ b/tests/integration/synology.test.ts @@ -170,7 +170,7 @@ describe("GET /nic/update — D1 logging", () => { expect(results[0].source).toBe("synology"); }); - it("still updates DNS when the log table is missing", async () => { + it("recreates the log schema on first write when the table is missing", async () => { await dropLogSchema(env.DB); const response = await SELF.fetch(makeSynologyUrl({ myip: "198.51.100.25" })); @@ -180,10 +180,14 @@ describe("GET /nic/update — D1 logging", () => { await new Promise((r) => setTimeout(r, 50)); const { results } = await env.DB.prepare( - "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'ddns_logs'", + "SELECT hostname, ip, action, source FROM ddns_logs ORDER BY id DESC LIMIT 1", ).all(); - expect(results).toHaveLength(0); + expect(results).toHaveLength(1); + expect(results[0].hostname).toBe("nas.example.com"); + expect(results[0].ip).toBe("198.51.100.25"); + expect(results[0].action).toBe("created"); + expect(results[0].source).toBe("synology"); }); }); diff --git a/tests/integration/update.test.ts b/tests/integration/update.test.ts index 5fe1710..2a70e76 100644 --- a/tests/integration/update.test.ts +++ b/tests/integration/update.test.ts @@ -250,7 +250,6 @@ describe("POST /update — D1 logging", () => { }); it("recreates the log schema on first write when the table is missing", async () => { - it("still updates DNS when the log table is missing", async () => { await dropLogSchema(env.DB); const response = await SELF.fetch(makeUpdateRequest({ ip: "198.51.100.21" })); @@ -259,9 +258,13 @@ describe("POST /update — D1 logging", () => { await new Promise((r) => setTimeout(r, 50)); const { results } = await env.DB.prepare( - "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'ddns_logs'", + "SELECT hostname, ip, action, source FROM ddns_logs ORDER BY id DESC LIMIT 1", ).all(); - expect(results).toHaveLength(0); + expect(results).toHaveLength(1); + expect(results[0].hostname).toBe("nas.example.com"); + expect(results[0].ip).toBe("198.51.100.21"); + expect(results[0].action).toBe("created"); + expect(results[0].source).toBe("api"); }); }); diff --git a/tests/prepare-deploy-config.test.ts b/tests/prepare-deploy-config.test.ts new file mode 100644 index 0000000..7601060 --- /dev/null +++ b/tests/prepare-deploy-config.test.ts @@ -0,0 +1,44 @@ +// @vitest-environment node + +import fs from "node:fs/promises"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +import { afterEach, describe, expect, it } from "vitest"; + +import { prepareDeployConfig } from "../scripts/prepare-deploy-config"; + +const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); +const generatedDeployDir = path.join(projectRoot, ".wrangler", "deploy"); +const generatedConfigPath = path.join(generatedDeployDir, "wrangler.generated.jsonc"); +const redirectConfigPath = path.join(generatedDeployDir, "config.json"); + +const D1_DATABASE_ID_ENV = "DDNS_D1_DATABASE_ID"; +const TEST_DATABASE_ID = "123e4567-e89b-12d3-a456-426614174000"; + +async function clearGeneratedDeployConfig(): Promise { + await fs.rm(generatedConfigPath, { force: true }); + await fs.rm(redirectConfigPath, { force: true }); + await fs.rm(generatedDeployDir, { recursive: true, force: true }); +} + +describe("prepareDeployConfig", () => { + afterEach(async () => { + delete process.env[D1_DATABASE_ID_ENV]; + await clearGeneratedDeployConfig(); + }); + + it("rewrites the generated main entrypoint relative to .wrangler/deploy", async () => { + process.env[D1_DATABASE_ID_ENV] = TEST_DATABASE_ID; + + await expect(prepareDeployConfig()).resolves.toBe(true); + + const generatedConfig = JSON.parse(await fs.readFile(generatedConfigPath, "utf8")) as { + main?: string; + d1_databases?: Array<{ database_id?: string }>; + }; + + expect(generatedConfig.main).toBe("../../src/index.ts"); + expect(generatedConfig.d1_databases?.[0]?.database_id).toBe(TEST_DATABASE_ID); + }); +}); \ No newline at end of file -- 2.51.2