import { describe, expect, it } from "vitest"; import { packageSecretDeclarations, parseSecretDeclarations } from "./secrets.js"; const secret = { name: "ACME_API_KEY", title: "Acme API key", description: "Create a read-only key in Acme settings.", required: true, }; describe("secret declarations", () => { it("keeps legacy packages unchanged", () => { expect(packageSecretDeclarations({ notion: { name: "Old app" } })).toEqual([]); }); it("defaults an omitted required flag to true", () => { expect( parseSecretDeclarations([ { name: "ACME_API_KEY", title: "Acme API key", description: "Create a read-only key in Acme settings.", }, ]), ).toEqual([secret]); }); it("trims help text and preserves names, order, and optional flags", () => { expect( parseSecretDeclarations([ { ...secret, title: " Acme key ", description: " First step.\nSecond step. ", required: false, }, ]), ).toEqual([ { name: "ACME_API_KEY", title: "Acme key", description: "First step.\nSecond step.", required: false, }, ]); }); it("accepts the exact UTF-16 length boundaries", () => { expect( parseSecretDeclarations([ { ...secret, name: "A".repeat(64), title: "😀".repeat(40), description: "😀".repeat(256), }, ]), ).toHaveLength(1); }); it.each([ ["name", "A".repeat(65)], ["name", " KEY"], ["name", "KEY "], ["name", "1KEY"], ["name", "notion_internal"], ["title", "a".repeat(81)], ["title", "😀".repeat(40) + "a"], ["title", " "], ["title", "Title\n"], ["title", "Title\u2028"], ["title", "Title\t"], ["description", "a".repeat(513)], ["description", "😀".repeat(256) + "a"], ["description", " "], ["description", "\u0000bad"], ["required", "true"], ["required", null], ["required", 0], ["required", undefined], ])("rejects invalid %s metadata", (field, value) => { expect(() => parseSecretDeclarations([{ ...secret, [field]: value }])).toThrow( /notion.secrets\[0\]/, ); }); it.each([ null, {}, [null], [{ ...secret, value: "DO_NOT_ECHO" }], [ { name: "ACME_API_KEY", title: "Acme API key", description: "Create a read-only key in Acme settings.", value: "DO_NOT_ECHO", }, ], [{ name: "KEY" }], ])("rejects malformed declarations without echoing their contents", (value) => { expect(() => parseSecretDeclarations(value)).toThrow(/notion.secrets/); try { parseSecretDeclarations(value); } catch (error) { expect(String(error)).not.toContain("DO_NOT_ECHO"); } }); it("reports an exact duplicate at the second declaration", () => { expect(() => parseSecretDeclarations([secret, secret])).toThrow( "notion.secrets[1].name must be unique", ); }); it("keeps case-distinct names and reserved-prefix exceptions", () => { expect( parseSecretDeclarations([ secret, { ...secret, name: "acme_api_key" }, { ...secret, name: "NOTION_API_TOKEN" }, ]), ).toHaveLength(3); }); });