import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { resolveRuntimeInput } from "./runtime-input.js"; let inputDirectory: string | undefined; afterEach(async () => { vi.unstubAllEnvs(); if (inputDirectory !== undefined) { await rm(inputDirectory, { recursive: true, force: true }); inputDirectory = undefined; } }); describe("resolveRuntimeInput", () => { it("returns the inline input when no payload path is configured", async () => { const fallback = { state: { cursor: "inline" } }; await expect(resolveRuntimeInput(fallback)).resolves.toBe(fallback); }); it("reads and parses the configured input payload", async () => { const fileInput = { state: { cursor: "file" } }; const inputPath = await writeInputPayload(JSON.stringify(fileInput)); vi.stubEnv("NOTION_INPUT_PAYLOAD_PATH", inputPath); await expect(resolveRuntimeInput({ state: { cursor: "inline" } })).resolves.toEqual( fileInput, ); }); it("returns the inline input when the configured payload is missing", async () => { const fallback = { state: { cursor: "inline" } }; const directory = await createInputDirectory(); vi.stubEnv("NOTION_INPUT_PAYLOAD_PATH", join(directory, "missing.json")); await expect(resolveRuntimeInput(fallback)).resolves.toBe(fallback); }); it("rejects malformed input payloads", async () => { const inputPath = await writeInputPayload("{"); vi.stubEnv("NOTION_INPUT_PAYLOAD_PATH", inputPath); await expect(resolveRuntimeInput(undefined)).rejects.toBeInstanceOf(SyntaxError); }); }); async function writeInputPayload(contents: string): Promise { const directory = await createInputDirectory(); const inputPath = join(directory, "input.json"); await writeFile(inputPath, contents); return inputPath; } async function createInputDirectory(): Promise { inputDirectory = await mkdtemp(join(tmpdir(), "notion-apps-input-")); return inputDirectory; }