Files
velum-raycast/tests/project-jobs.test.ts

74 lines
3.3 KiB
TypeScript

import { expect, test } from "bun:test";
import { execFile, spawn } from "node:child_process";
import { once } from "node:events";
import { promises as fs } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { promisify } from "node:util";
const exec = promisify(execFile);
test("the saved project job survives termination of its launching process", async () => {
const temporary = await fs.mkdtemp(join(tmpdir(), "velum-worker-test-"));
const launcher = join(temporary, "launcher.cjs");
const assets = join(temporary, "assets");
const support = join(temporary, "support");
const root = join(temporary, "project");
await fs.mkdir(assets);
const runtime = resolve("src/lib/worker-runtime.ts");
const jobs = resolve("src/lib/project-jobs.ts");
let parent: ReturnType<typeof spawn> | undefined;
try {
await fs.writeFile(join(assets, "velum-project-worker.cjs"), `
const fs = require("node:fs");
const path = require("node:path");
process.stdin.resume();
process.stdin.once("end", () => setTimeout(() => {
fs.writeFileSync(path.join(process.argv[2], "state.json"), JSON.stringify({ phase: "review" }), { mode: 0o600 });
}, 400));
`);
const entry = join(temporary, "launcher.ts");
await fs.writeFile(entry, `
import { configureWorker } from ${JSON.stringify(runtime)};
import { projectJobDirectory, startProjectAnalysis } from ${JSON.stringify(jobs)};
configureWorker({
assets: ${JSON.stringify(assets)}, support: ${JSON.stringify(support)},
preferences: { velumBaseUrl: "https://example.invalid", authentikTokenUrl: "https://example.invalid/token",
clientId: "test", serviceAccountUsername: "test", serviceAccountPassword: "synthetic-secret" },
});
const root = ${JSON.stringify(root)};
startProjectAnalysis({ root, files: [], rules: [], whitelist: [], entityTypes: ["PERSON"], skipped: [],
reanalyzeUnchanged: false, imagePolicy: "placeholder", fileSelectionChanged: false }).then(() => {
console.log(projectJobDirectory(root));
setInterval(() => {}, 1_000);
});
`);
await exec(resolve("node_modules/.bin/esbuild"), [entry, "--bundle", "--platform=node",
`--alias:@raycast/api=${runtime}`, `--outfile=${launcher}`]);
const child = spawn(process.execPath, [launcher], { stdio: ["ignore", "pipe", "inherit"] });
parent = child;
const terminated = once(child, "exit");
const [pending] = await Promise.race([
once(child.stdout, "data"),
terminated.then(() => { throw new Error("Projektauftrag konnte nicht gestartet werden."); }),
]);
const directory = String(pending).trim();
child.kill("SIGKILL");
await terminated;
const state = join(directory, "state.json");
let phase: string | undefined;
for (let attempt = 0; attempt < 50; attempt++) {
phase = JSON.parse(await fs.readFile(state, "utf8")).phase;
if (phase === "review") break;
await new Promise((done) => setTimeout(done, 40));
}
expect(phase).toBe("review");
const input = join(directory, "input.json");
expect(await fs.readFile(input, "utf8")).not.toContain("synthetic-secret");
expect((await fs.stat(input)).mode & 0o777).toBe(0o600);
} finally {
parent?.kill();
await fs.rm(temporary, { recursive: true, force: true });
}
});