import { describe, expect, test } from "bun:test"; import { addPendingRename, cancelPendingRename, completeRename } from "../server/utils/renames"; import { provisionalTopicTitle } from "../utils/topic-title"; describe("rename attempt ownership", () => { test("an old completion cannot remove a newer attempt", () => { addPendingRename("topic", "old"); const current = addPendingRename("topic", "new"); completeRename("topic", "old"); cancelPendingRename("topic", "new"); expect(current.signal.aborted).toBe(true); }); test("an old cancellation cannot abort a newer attempt", () => { const current = addPendingRename("topic", "new"); cancelPendingRename("topic", "old"); expect(current.signal.aborted).toBe(false); completeRename("topic", "new"); }); test("cancellation is safe after completion or restart", () => { cancelPendingRename("missing", "old"); const current = addPendingRename("topic", "new"); completeRename("topic", "new"); cancelPendingRename("topic", "new"); expect(current.signal.aborted).toBe(false); }); }); describe("provisional titles", () => { test("uses the first meaningful line and normalizes whitespace", () => { expect(provisionalTopicTitle({ content: "\n Help me debug this\nMore details" })).toBe("Help me debug this"); }); test("names attachment-only conversations", () => { expect(provisionalTopicTitle({ content: "", files: [{ name: "photo.jpg" }] })).toBe("photo.jpg"); expect(provisionalTopicTitle({ content: "", fileIds: ["image"] })).toBe("Attached files"); }); test("bounds title length and handles empty input", () => { expect(provisionalTopicTitle({ content: "a".repeat(200) })).toHaveLength(100); expect(provisionalTopicTitle()).toBe("New Topic"); }); });