// MUST be first: sets DATABASE_URL before db_client is evaluated. // watchers.ts imports ./db transitively, and db_client throws at import time on // a missing DATABASE_URL — an assignment at the top of THIS file would run too // late, because ES imports hoist. import "./setup_env"; import { describe, expect, it } from "bun:test"; import { parseWatcherList, planWatcherAttach } from "../src/watchers"; describe("parseWatcherList", () => { it("treats an omitted field as 'no opinion', not an empty list", () => { // undefined ≠ [] downstream: extraWatchers: undefined keeps the default // watchers (importer + Meego assignee + shared group). expect(parseWatcherList(undefined)).toEqual({ ok: true, watchers: undefined }); }); it("canonicalises email prefixes and passes Lark ids through", () => { expect(parseWatcherList(["alice", "bob@bytedance.com", "oc_abc", "ou_def"])).toEqual({ ok: true, watchers: ["alice@bytedance.com", "bob@bytedance.com", "oc_abc", "ou_def"], }); }); it("drops entries that normalise to nothing instead of failing the call", () => { // A junk entry must not cost the caller the whole task creation. expect(parseWatcherList([" ", "!!", "alice"])).toEqual({ ok: true, watchers: ["alice@bytedance.com"] }); }); it("accepts an explicit empty list", () => { expect(parseWatcherList([])).toEqual({ ok: true, watchers: [] }); }); for (const bad of ["oc_abc", 42, null, { chat: "oc_abc" }, ["oc_abc", 7]]) { it(`rejects ${JSON.stringify(bad)} — must be a string[]`, () => { expect(parseWatcherList(bad)).toEqual({ ok: false, error: "watchers must be a string[]" }); }); } }); describe("planWatcherAttach", () => { const owned = { createdViaKeyId: "opk_aaa", watchers: ["dave@bytedance.com", "oc_shared"] }; it("does nothing when the caller named no watchers", () => { expect(planWatcherAttach(owned, undefined, "opk_aaa")).toEqual({ kind: "noop" }); expect(planWatcherAttach(owned, [], "opk_aaa")).toEqual({ kind: "noop" }); }); it("appends to the existing list for a task this key created", () => { expect(planWatcherAttach(owned, ["oc_group"], "opk_aaa")).toEqual({ kind: "attach", keys: ["dave@bytedance.com", "oc_shared", "oc_group"], }); }); it("handles a task with no watchers yet", () => { expect(planWatcherAttach({ createdViaKeyId: "opk_aaa", watchers: null }, ["oc_group"], "opk_aaa")).toEqual({ kind: "attach", keys: ["oc_group"], }); }); // The leak guard. A watcher is a delivery target for this task's status cards // and fix report, so "anyone holding a key may subscribe any chat to any task // whose Meego link they know" is an exfiltration path, not a convenience. it("refuses a task created by a different key", () => { expect(planWatcherAttach({ ...owned, createdViaKeyId: "opk_bbb" }, ["oc_group"], "opk_aaa")).toEqual({ kind: "forbidden" }); }); it("refuses a task with no open-platform provenance (panel-imported)", () => { expect(planWatcherAttach({ ...owned, createdViaKeyId: null }, ["oc_group"], "opk_aaa")).toEqual({ kind: "forbidden" }); expect(planWatcherAttach({ watchers: owned.watchers }, ["oc_group"], "opk_aaa")).toEqual({ kind: "forbidden" }); }); });