// MUST be first: points DATABASE_URL at a closed local port before anything // could evaluate db_client, so this test can't reach production. import "./setup_env"; import { appendFileSync, mkdtempSync, readFileSync, rmSync, statSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterAll, describe, expect, test } from "bun:test"; import { appendWish, readWishes, submitWish, wishLogPath, WISH_DAY_MAX, WISH_GAP_MS, WISH_MAX_CHARS, type WishEntry, type WishLog } from "../src/wishes"; const sinks = () => { const recorded: WishEntry[] = []; const notified: WishEntry[] = []; return { recorded, notified, record: async (e: WishEntry) => { recorded.push(e); }, notify: async (e: WishEntry) => { notified.push(e); return true; }, }; }; describe("submitWish", () => { test("needs a signed-in user", async () => { const s = sinks(); const r = await submitWish({ email: null, text: "hi", now: 1 }, new Map(), s); expect(r.status).toBe(401); expect(s.recorded).toHaveLength(0); }); test("rejects empty or non-string text", async () => { const s = sinks(); expect((await submitWish({ email: "a@x.com", text: " ", now: 1 }, new Map(), s)).status).toBe(400); expect((await submitWish({ email: "a@x.com", text: 42, now: 1 }, new Map(), s)).status).toBe(400); expect(s.recorded).toHaveLength(0); }); test("records a trimmed, capped wish and notifies", async () => { const s = sinks(); const r = await submitWish({ email: "a@x.com", text: ` ${"许".repeat(WISH_MAX_CHARS + 50)} `, now: Date.UTC(2026, 10, 5) }, new Map(), s); expect(r).toEqual({ status: 200, body: { ok: true } }); expect(s.recorded).toHaveLength(1); expect(s.recorded[0].text.length).toBe(WISH_MAX_CHARS); expect(s.recorded[0]).toMatchObject({ email: "a@x.com", at: "2026-11-05T00:00:00.000Z" }); await Promise.resolve(); expect(s.notified).toHaveLength(1); }); test("works without a Lark group (record only)", async () => { const s = sinks(); const r = await submitWish({ email: "a@x.com", text: "hello", now: 1 }, new Map(), { record: s.record }); expect(r.status).toBe(200); expect(s.recorded).toHaveLength(1); }); test("throttles: one per gap, a daily cap, per person", async () => { const s = sinks(); const log: WishLog = new Map(); const t0 = 1_000_000_000; expect((await submitWish({ email: "a@x.com", text: "1", now: t0 }, log, s)).status).toBe(200); expect((await submitWish({ email: "a@x.com", text: "2", now: t0 + 1000 }, log, s)).status).toBe(429); expect((await submitWish({ email: "b@x.com", text: "other person", now: t0 + 1000 }, log, s)).status).toBe(200); let t = t0; for (let i = 1; i < WISH_DAY_MAX; i++) { t += WISH_GAP_MS; expect((await submitWish({ email: "a@x.com", text: `w${i}`, now: t }, log, s)).status).toBe(200); } expect((await submitWish({ email: "a@x.com", text: "over", now: t + WISH_GAP_MS }, log, s)).status).toBe(429); // a day after the first one, room opens again expect((await submitWish({ email: "a@x.com", text: "next day", now: t0 + 86_400_000 + 1 }, log, s)).status).toBe(200); }); test("a failed record is a 500 and doesn't count against the throttle", async () => { const log: WishLog = new Map(); const bad = { record: async () => { throw new Error("disk full"); } }; expect((await submitWish({ email: "a@x.com", text: "x", now: 1 }, log, bad)).status).toBe(500); const s = sinks(); expect((await submitWish({ email: "a@x.com", text: "x", now: 2 }, log, s)).status).toBe(200); }); }); describe("appendWish", () => { const dir = mkdtempSync(path.join(tmpdir(), "wishes-")); afterAll(() => rmSync(dir, { recursive: true, force: true })); test("appends one JSON line per wish to an owner-only file", async () => { const file = path.join(dir, "nested", "wishes.jsonl"); await appendWish({ at: "2026-11-05T00:00:00.000Z", email: "a@x.com", text: "第一个愿望" }, file); await appendWish({ at: "2026-11-05T00:01:00.000Z", email: "b@x.com", text: "line\nbreak" }, file); const lines = readFileSync(file, "utf8").trim().split("\n"); expect(lines).toHaveLength(2); expect(JSON.parse(lines[0]).text).toBe("第一个愿望"); expect(JSON.parse(lines[1]).text).toBe("line\nbreak"); expect(statSync(file).mode & 0o777).toBe(0o600); }); test("path: WISH_LOG_PATH wins, else ~/.streamline/wishes.jsonl", () => { expect(wishLogPath({ WISH_LOG_PATH: "/tmp/w.jsonl" } as NodeJS.ProcessEnv)).toBe("/tmp/w.jsonl"); expect(wishLogPath({} as NodeJS.ProcessEnv).endsWith(path.join(".streamline", "wishes.jsonl"))).toBe(true); }); }); describe("readWishes", () => { const dir = mkdtempSync(path.join(tmpdir(), "wishes-read-")); afterAll(() => rmSync(dir, { recursive: true, force: true })); test("no file yet is an empty list", async () => { expect(await readWishes(path.join(dir, "missing.jsonl"))).toEqual([]); }); test("newest first, torn or malformed lines skipped", async () => { const file = path.join(dir, "wishes.jsonl"); await appendWish({ at: "2026-11-05T00:00:00.000Z", email: "a@x.com", text: "first" }, file); appendFileSync(file, '{"at":"2026-11-05T00:00:30.000Z","email":"x@x.com","te\n'); appendFileSync(file, '{"at":1,"email":"y@x.com","text":"wrong shape"}\n'); await appendWish({ at: "2026-11-05T00:01:00.000Z", email: "b@x.com", text: "multi\nline" }, file); expect(await readWishes(file)).toEqual([ { at: "2026-11-05T00:01:00.000Z", email: "b@x.com", text: "multi\nline" }, { at: "2026-11-05T00:00:00.000Z", email: "a@x.com", text: "first" }, ]); }); });