Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : tests/unit/next-tasks.test.ts9 * Module : tests10 * Repository : https://git.spboucher.ai/note-spboucher-ai11 * Project Website : https://note.spboucher.ai12 * Created : 2026-11-1913 * Last Modified : 2026-11-1914 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * Tasks 3.0 (SPEC §4.2): client-chosen id and idempotent creation, anchors, due dates, soft delete, deltas, journal events, and the45 * link between a `taskItem` of a document and its task. Runs against a *_dev database.46 */47import { test, before, after } from "node:test";48import assert from "node:assert/strict";49import { randomUUID } from "node:crypto";50import { eq } from "drizzle-orm";51import { db, schema } from "../../src/db/index";52import { workspace, mk, doc, made, cleanup, code, kinds, para } from "../helpers/next";53import { createTask, updateTask, deleteTask, getTask, listTasks, parseDue, restoreTask } from "../../src/lib/tasks/service";54import { taskCreate, taskPatch } from "../../src/lib/tasks/schema";55import { getPage, updatePage } from "../../src/lib/pages/service";56import type { PMNode } from "../../src/lib/editor/text";5758let ws: string; const PFX = "nx-task ·";59before(async () => { ws = await workspace(); }); after(() => cleanup(ws, PFX));60const taskRow = async (id: string) => (await db.select().from(schema.tasks).where(eq(schema.tasks.id, id)))[0];61const item = (text: string, attrs: Record<string, unknown>): PMNode => ({ type: "taskItem", attrs: { checked: false, ...attrs }, content: [para(text)] });62const list = (...items: PMNode[]): PMNode => doc({ type: "taskList", content: items }, "autre paragraphe");6364test("a client-chosen id creates the task once; a repeat returns the stored task untouched", async () => {65 const id = randomUUID(); made.tasks.push(id); const p = await mk(ws, `${PFX} page`, doc("x"));66 const a = await createTask(ws, { id, title: `${PFX} offline`, priority: "high", dueAt: "2026-12-01", pageId: p.id, blockId: "blk42" });67 assert.equal(a.existing, false); assert.equal(a.task.id, id); assert.equal(a.task.blockId, "blk42"); assert.equal(a.task.priority, "high"); assert.equal(a.task.dueAt?.toISOString(), "2026-12-01T12:00:00.000Z"); assert.ok(a.task.rev > 0);68 await updateTask(ws, id, { status: "done" });69 const b = await createTask(ws, { id, title: "another title", priority: "low" }); assert.equal(b.existing, true); assert.equal(b.task.title, `${PFX} offline`); assert.equal(b.task.status, "done", "never overwritten by a retry");70 await deleteTask(ws, id); const c = await createTask(ws, { id, title: "x" }); assert.equal(c.existing, true); assert.ok(c.task.deletedAt, "a deleted task is not resurrected by a retry");71 assert.equal((await kinds(p.id)).filter((k) => k === "task.created").length, 1, "one journal entry for one creation");72});73test("validation: unknown page → 400 bad_page, bad due date → 400, body schemas", async () => {74 assert.equal(await code(createTask(ws, { title: "x", pageId: randomUUID() })), "bad_page");75 const { task } = await createTask(ws, { title: `${PFX} v` }); made.tasks.push(task.id);76 assert.equal(await code(updateTask(ws, task.id, { pageId: randomUUID() })), "bad_page"); assert.equal(await code(updateTask(ws, randomUUID(), { title: "x" })), "not_found");77 assert.throws(() => parseDue("demain"), /Invalid due date/); assert.equal(parseDue(null), null); assert.equal(parseDue("2026-11-25T09:00:00-05:00")?.toISOString(), "2026-11-25T14:00:00.000Z");78 assert.equal(taskCreate.safeParse({ title: "x", id: "nope" }).success, false); assert.equal(taskCreate.safeParse({ title: "" }).success, false);79 assert.equal(taskCreate.safeParse({ title: "x", due: "2026-11-25", priority: "high", blockId: "abc", pageId: null }).success, true); assert.equal(taskCreate.safeParse({ title: "x", dueAt: "25/11/2026" }).success, false);80 assert.equal(taskPatch.safeParse({ status: "doing" }).success, true); assert.equal(taskPatch.safeParse({ priority: "urgent" }).success, false);81});82test("complete / reopen are journaled once each; delete is soft and shows in a delta", async () => {83 const p = await mk(ws, `${PFX} events`, doc("x")); const { task } = await createTask(ws, { title: `${PFX} e`, pageId: p.id }); made.tasks.push(task.id);84 const r0 = task.rev; const done = await updateTask(ws, task.id, { status: "done" }); assert.ok(done.completedAt); assert.ok(done.rev > r0);85 await updateTask(ws, task.id, { status: "done" }); await updateTask(ws, task.id, { title: `${PFX} e2` }); const re = await updateTask(ws, task.id, { status: "todo" }); assert.equal(re.completedAt, null);86 assert.deepEqual((await kinds(p.id)).filter((k) => k.startsWith("task.")), ["task.created", "task.completed", "task.reopened"]);87 assert.equal(await deleteTask(ws, task.id), true); assert.equal(await deleteTask(ws, task.id), false, "second delete: nothing left to delete");88 assert.equal(await code(getTask(ws, task.id)), "not_found"); assert.ok((await getTask(ws, task.id, { includeDeleted: true })).deletedAt);89 assert.equal((await listTasks(ws)).some((t) => t.id === task.id), false); const delta = await listTasks(ws, { since: re.rev }); assert.ok(delta.find((t) => t.id === task.id)?.deletedAt, "the delta tells clients about the deletion");90 assert.equal((await restoreTask(ws, task.id)).deletedAt, null);91});92test("a taskItem with a new taskId creates its task when the page is saved", async () => {93 const id = randomUUID(); made.tasks.push(id);94 const p = await mk(ws, `${PFX} doc`, list(item("Écrire la section 3", { taskId: id, due: "2026-11-25", priority: "high" }), item("sans lien", {})));95 const t = await taskRow(id); assert.ok(t, "created from the document"); assert.equal(t.title, "Écrire la section 3"); assert.equal(t.pageId, p.id); assert.equal(t.status, "todo"); assert.equal(t.priority, "high"); assert.equal(t.dueAt?.toISOString(), "2026-11-25T12:00:00.000Z");96 const stored = (await getPage(ws, p.id)).content as PMNode; const blockId = stored.content![0].content![0].attrs!.id; assert.match(blockId, /^[\w-]{10}$/); assert.equal(t.blockId, blockId, "anchored to the taskItem block");97 assert.equal((await db.select().from(schema.tasks).where(eq(schema.tasks.pageId, p.id))).length, 1, "an item without taskId creates nothing");98 assert.ok((await kinds(p.id)).includes("task.created"));99});100test("checking the box completes the task; an unrelated save never undoes a task completed elsewhere", async () => {101 const id = randomUUID(); made.tasks.push(id); const p = await mk(ws, `${PFX} sync`, list(item("Tâche liée", { taskId: id })));102 const withChecked = (checked: boolean, text = "Tâche liée", tail = "autre paragraphe") => { const cur = list(item(text, { taskId: id, checked })); cur.content![1] = para(tail); return cur; };103 let v = p.version;104 v = (await updatePage(ws, p.id, { content: withChecked(true) }, { baseVersion: v })).version; assert.equal((await taskRow(id)).status, "done"); assert.ok((await taskRow(id)).completedAt);105 v = (await updatePage(ws, p.id, { content: withChecked(false) }, { baseVersion: v })).version; assert.equal((await taskRow(id)).status, "todo");106 assert.deepEqual((await kinds(p.id)).filter((k) => k === "task.completed" || k === "task.reopened"), ["task.completed", "task.reopened"]);107 // completed from the task list (API), then the page is saved again with an edit elsewhere and the box still unchecked in that copy108 await updateTask(ws, id, { status: "done" });109 v = (await updatePage(ws, p.id, { content: withChecked(false, "Tâche liée", "paragraphe modifié") }, { baseVersion: v })).version; assert.equal((await taskRow(id)).status, "done", "the document did not change that item: the task keeps its state");110 // renaming the item renames the task; removing the item keeps the task111 v = (await updatePage(ws, p.id, { content: withChecked(false, "Tâche renommée", "paragraphe modifié") }, { baseVersion: v })).version; assert.equal((await taskRow(id)).title, "Tâche renommée");112 await updatePage(ws, p.id, { content: doc("plus de liste") }, { baseVersion: v }); const t = await taskRow(id); assert.ok(t && !t.deletedAt, "removing the item does not delete the task");113});114test("a taskId that belongs to a deleted task is left alone", async () => {115 const { task } = await createTask(ws, { title: `${PFX} dead` }); made.tasks.push(task.id); await deleteTask(ws, task.id);116 const p = await mk(ws, `${PFX} dead doc`, list(item("zombie", { taskId: task.id, checked: true })));117 const t = await taskRow(task.id); assert.ok(t.deletedAt); assert.equal(t.status, "todo"); assert.equal(t.pageId, null); assert.equal((await kinds(p.id)).filter((k) => k.startsWith("task.")).length, 0);118});119