修复token缺失bug
This commit is contained in:
29
tests/deploy-env.test.ts
Normal file
29
tests/deploy-env.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { execFile } from "node:child_process";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
describe("deployment environment preparation", () => {
|
||||
it("adds a strong internal worker token when .env.local does not define one", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "zhinian-deploy-env-"));
|
||||
try {
|
||||
await writeFile(join(dir, ".env.example"), "APP_PORT=3000\nZHINIAN_INTERNAL_WORKER_TOKEN=change-me-worker-token\n");
|
||||
await writeFile(join(dir, ".env.local"), "APP_PORT=3001\n");
|
||||
|
||||
const { stdout } = await execFileAsync(process.execPath, ["scripts/ensure-deploy-env.mjs", dir], {
|
||||
cwd: new URL("../", import.meta.url)
|
||||
});
|
||||
const envLocal = await readFile(join(dir, ".env.local"), "utf8");
|
||||
|
||||
expect(stdout).toContain("Added ZHINIAN_INTERNAL_WORKER_TOKEN");
|
||||
expect(envLocal).toMatch(/^ZHINIAN_INTERNAL_WORKER_TOKEN=[a-f0-9]{64}$/m);
|
||||
expect(envLocal).toContain("APP_PORT=3001");
|
||||
} finally {
|
||||
await rm(dir, { force: true, recursive: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
24
tests/worker-script.test.ts
Normal file
24
tests/worker-script.test.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
const rootDir = fileURLToPath(new URL("../", import.meta.url));
|
||||
|
||||
describe("worker script configuration", () => {
|
||||
it("fails fast in production when the internal worker token is missing", async () => {
|
||||
await expect(execFileAsync(process.execPath, ["scripts/worker.mjs", "--once"], {
|
||||
cwd: rootDir,
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: "production",
|
||||
ZHINIAN_INTERNAL_WORKER_TOKEN: "",
|
||||
ZHINIAN_WORKER_BASE_URL: "http://127.0.0.1:9"
|
||||
}
|
||||
})).rejects.toMatchObject({
|
||||
code: 1,
|
||||
stderr: expect.stringContaining("ZHINIAN_INTERNAL_WORKER_TOKEN is required")
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user