import { describe, expect, it } from "vitest"; import { addTodo, parseAddRequest, parseToggleRequest, toggleTodo, visibleTodos, } from "../shared/todo"; describe("todo service", () => { it("adds with increasing id and trims title", () => { const one = addTodo([], " 写测试 "); expect(one[0]).toEqual({ id: 1, title: "写测试", done: false }); const two = addTodo(one, "第二条"); expect(two[1]?.id).toBe(2); }); it("rejects empty and too-long titles", () => { expect(() => addTodo([], " ")).toThrow("BAD_TITLE:empty"); expect(() => addTodo([], "x".repeat(81))).toThrow("BAD_TITLE:too-long"); }); it("toggles only the matching id", () => { const todos = [ { id: 1, title: "a", done: false }, { id: 2, title: "b", done: false }, ]; expect(toggleTodo(todos, 1)[0]?.done).toBe(true); expect(toggleTodo(todos, 9)).toEqual(todos); }); it("filters active and done without mutating", () => { const todos = [ { id: 1, title: "a", done: true }, { id: 2, title: "b", done: false }, ]; expect(visibleTodos(todos, "active").map((row) => row.id)).toEqual([2]); expect(visibleTodos(todos, "done").map((row) => row.id)).toEqual([1]); const copy = visibleTodos(todos, "all"); copy.push({ id: 3, title: "c", done: false }); expect(todos).toHaveLength(2); }); it("validates IPC payloads", () => { expect(() => parseAddRequest({ title: 1 })).toThrow("BAD_TITLE:not-string"); expect(() => parseAddRequest({ title: "" })).toThrow("BAD_TITLE:empty"); expect(() => parseToggleRequest({ id: "1" })).toThrow("BAD_ID"); expect(() => parseToggleRequest({ id: 0 })).toThrow("BAD_ID"); expect(() => parseToggleRequest({ id: 2 })).not.toThrow(); }); });