import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { readRunMetadata } from "./runtime-metadata.js"; const temporaryDirectories: string[] = []; function temporaryPath(name: string): string { const directory = fs.mkdtempSync(path.join(os.tmpdir(), "notion-apps-metadata-")); temporaryDirectories.push(directory); return path.join(directory, name); } afterEach(() => { vi.unstubAllEnvs(); for (const directory of temporaryDirectories.splice(0)) { fs.rmSync(directory, { recursive: true, force: true }); } }); describe("readRunMetadata", () => { it("throws when the metadata path is not configured", () => { vi.stubEnv("NOTION_RUN_METADATA_PATH", undefined); expect(() => readRunMetadata()).toThrow("Workflow run metadata path is not configured"); }); it("reads workflow run metadata", () => { const metadataPath = temporaryPath("metadata.json"); const metadata = { workspaceId: "workspace-id", workerId: "worker-id", runGroupId: "run-group-id", runId: "run-id", capabilityType: "workflow" as const, attemptNumber: 2, }; fs.writeFileSync(metadataPath, JSON.stringify(metadata)); vi.stubEnv("NOTION_RUN_METADATA_PATH", metadataPath); expect(readRunMetadata()).toEqual(metadata); }); it("rejects invalid workflow run metadata", () => { const metadataPath = temporaryPath("metadata.json"); fs.writeFileSync(metadataPath, JSON.stringify({ runId: "run-id" })); vi.stubEnv("NOTION_RUN_METADATA_PATH", metadataPath); expect(() => readRunMetadata()).toThrow("Invalid workflow run metadata"); }); });