SPB Git

Repository Public

next-p-ra-ui.test.ts 12.1 KB 150 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/unit/next-p-ra-ui.test.ts9 * Description      : Pure logic behind the P-RA UI fixes: task quick-add10 *                    grammar (U12: no date read out of a title word, valid11 *                    times only, "next <weekday>") and the paging helpers of12 *                    the trash / archive / search lists (U10). No browser, no13 *                    database.14 * Module           : tests15 * Repository       : https://git.spboucher.ai/note-spboucher-ai16 * Project Website  : https://note.spboucher.ai17 * Created          : 2026-11-1918 * Last Modified    : 2026-11-1919 *20 * Author           : Simon-Pierre Boucher21 * Position         : Professor of Finance22 * Institution      : Université du Québec en Outaouais (UQO)23 * Organization     : SP Boucher24 * Website          : https://spboucher.ai25 * Git              : https://git.spboucher.ai26 *27 * Preferred Contact: contact@spboucher.ai28 * Development      : dev@spboucher.ai29 * Support          : support@spboucher.ai30 * Security/Admin   : admin@spboucher.ai31 *32 * Copyright (c) 202633 * Simon-Pierre Boucher34 * All rights reserved where applicable. This file is governed by the35 * applicable project license, copyright notices and relevant law.36 * Existing third-party rights and notices remain unaffected.37 *38 * License          : None specified (no license file in this repository)39 *40 * Do not embed credentials, private keys, access tokens,41 * certificates or confidential personal information.42 *43 * =============================================================================44 * END OF SP BOUCHER SOURCE HEADER45 * =============================================================================46 */4748import { test } from "node:test";49import assert from "node:assert/strict";50import { parseTaskInput, nextWeekday } from "../../src/components/tasks/parse";51import { LIST_PAGE_SIZE, MAX_OFFSET, SEARCH_MAX_LIMIT, SEARCH_PAGE_SIZE, flattenPages, nextOffset, parseOffset, searchPaging } from "../../src/lib/paging";5253/** Wednesday 18 November 2026, 10:00 local time. */54const NOW = new Date(2026, 10, 18, 10, 0, 0);55const p = (s: string) => parseTaskInput(s, NOW);56/** Local "YYYY-MM-DD HH:mm" of a due date (the parser works in local time). */57const fmt = (d: Date | null) => { if (!d) return null; const z = (n: number) => String(n).padStart(2, "0"); return `${d.getFullYear()}-${z(d.getMonth() + 1)}-${z(d.getDate())} ${z(d.getHours())}:${z(d.getMinutes())}`; };58const due = (s: string) => fmt(p(s).dueAt);5960test("the fixed clock is a Wednesday", () => { assert.equal(NOW.getDay(), 3); });6162test("U12: words of a title are never read as a date", () => {63  for (const title of ["Buy sun cream", "Plan 3 days in Paris", "Update sat nav maps", "Read mon oncle review", "Trip lasts 3 days", "Watch the wed episode again", "Plan weekend trip to Quebec", "Book friday night dinner table", "Compare may 5 and june 6 drafts", "Check 3/4 of the proofs today's way", "Pay sun-life invoice", "Sunday-roast recipe draft", "Turn the lights on", "Stand by", "Fix 2026-13-45 parser bug", "Feb 31 does not exist"]) {64    const r = p(title); assert.equal(r.dueAt, null, title); assert.equal(r.title, title, title);65  }66  // an explicit marker does not rescue an abbreviation that is a plain word inside the title67  const r = p("Put on sun cream"); assert.equal(r.dueAt, null); assert.equal(r.title, "Put on sun cream");68});6970test("U12: self-explanatory phrases and marked dates are read anywhere", () => {71  assert.deepEqual([p("Call Marc tomorrow about the grant").title, due("Call Marc tomorrow about the grant")], ["Call Marc about the grant", "2026-11-19 17:00"]);72  assert.deepEqual([p("Review today the proofs").title, due("Review today the proofs")], ["Review the proofs", "2026-11-18 18:00"]);73  assert.deepEqual([p("Review in 3 days the draft").title, due("Review in 3 days the draft")], ["Review the draft", "2026-11-21 17:00"]);74  assert.equal(due("Review in a week"), "2026-11-25 17:00"); assert.equal(due("Renew passport in two months"), "2027-01-18 17:00");75  assert.deepEqual([p("Lunch on friday with Bob").title, due("Lunch on friday with Bob")], ["Lunch with Bob", "2026-11-20 17:00"]);76  assert.deepEqual([p("Report due fri for the dean").title, due("Report due fri for the dean")], ["Report for the dean", "2026-11-20 17:00"]);77  assert.deepEqual([p("Send by nov 30 the abstract").title, due("Send by nov 30 the abstract")], ["Send the abstract", "2026-11-30 17:00"]);78  assert.deepEqual([p("Pay rent due tomorrow").title, due("Pay rent due tomorrow")], ["Pay rent", "2026-11-19 17:00"]);79  assert.deepEqual([p("Renew 2026-12-01 licence").title, due("Renew 2026-12-01 licence")], ["Renew licence", "2026-12-01 17:00"]);80  assert.equal(due("Tidy up this weekend the garage"), "2026-11-21 17:00"); assert.equal(due("Close books end of month please"), "2026-11-30 17:00"); assert.equal(due("Ship end of the week"), "2026-11-20 17:00");81  assert.equal(due("Plan next month budget"), "2026-12-18 17:00");82});8384test("U12: a bare date counts only as the last words, or (full names and calendar dates) the first words", () => {85  assert.deepEqual([p("Call Marc friday").title, due("Call Marc friday")], ["Call Marc", "2026-11-20 17:00"]);86  assert.deepEqual([p("Pay rent fri").title, due("Pay rent fri")], ["Pay rent", "2026-11-20 17:00"]);87  assert.deepEqual([p("Buy cream sun").title, due("Buy cream sun")], ["Buy cream", "2026-11-22 17:00"]);88  assert.deepEqual([p("Call mom on sun").title, due("Call mom on sun")], ["Call mom", "2026-11-22 17:00"]);89  assert.deepEqual([p("Submit abstract nov 15").title, due("Submit abstract nov 15")], ["Submit abstract", "2027-11-15 17:00"], "already past this year: next year");90  assert.deepEqual([p("Submit abstract november 30th, 2026").title, due("Submit abstract november 30th, 2026")], ["Submit abstract", "2026-11-30 17:00"]);91  assert.equal(due("Submit abstract 15 dec"), "2026-12-15 17:00"); assert.equal(due("Submit abstract 12/15"), "2026-12-15 17:00"); assert.equal(due("Submit abstract 15/12/2026"), "2026-12-15 17:00");92  assert.deepEqual([p("Monday call Marc").title, due("Monday call Marc")], ["call Marc", "2026-11-23 17:00"]);93  assert.deepEqual([p("Dec 1 renew licence").title, due("Dec 1 renew licence")], ["renew licence", "2026-12-01 17:00"]);94  assert.deepEqual([p("Clean garage weekend").title, due("Clean garage weekend")], ["Clean garage", "2026-11-21 17:00"]);95  assert.equal(due("Sun cream buy"), null, "an abbreviation opening the text is a word"); assert.equal(due("Mon oncle review"), null);96  assert.equal(due("wednesday"), "2026-11-25 17:00", "the same weekday is next week's, never today"); assert.equal(p("wednesday").title, "wednesday", "a title is never empty");97});9899test("U12: 'next <weekday>' is the weekday of next calendar week; a bare weekday is the coming one", () => {100  assert.equal(due("Call Marc friday"), "2026-11-20 17:00"); assert.equal(due("Call Marc next friday"), "2026-11-27 17:00");101  assert.equal(due("Call Marc sunday"), "2026-11-22 17:00"); assert.equal(due("Call Marc next sunday"), "2026-11-29 17:00");102  assert.equal(due("Call Marc monday"), "2026-11-23 17:00"); assert.equal(due("Call Marc next monday"), "2026-11-23 17:00", "the coming Monday already is next week's");103  assert.equal(due("Call Marc next tue"), "2026-11-24 17:00"); assert.equal(due("next week plan sprint"), "2026-11-23 17:00");104  assert.equal(p("Call Marc next friday").title, "Call Marc");105  const sunday = new Date(2026, 10, 22, 9, 0); assert.equal(fmt(nextWeekday(sunday, 1)), "2026-11-23 00:00"); assert.equal(fmt(nextWeekday(sunday, 0)), "2026-11-29 00:00");106  const monday = new Date(2026, 10, 16, 9, 0); assert.equal(fmt(nextWeekday(monday, 1)), "2026-11-23 00:00"); assert.equal(fmt(parseTaskInput("x monday", monday).dueAt), "2026-11-23 17:00");107});108109test("U12: times are validated (0–23 h, 0–59 min, 1–12 with am/pm); an invalid one stays in the title", () => {110  for (const title of ["Meet at 99", "Meet at 24", "Meet at 9:75", "Meet 13pm", "Meet 0am", "Meet at 12:60pm"]) { const r = p(title); assert.equal(r.dueAt, null, title); assert.equal(r.title, title, title); }111  assert.deepEqual([p("Meet at 23").title, due("Meet at 23")], ["Meet", "2026-11-18 23:00"]);112  assert.deepEqual([p("Meet at 0:05").title, due("Meet at 0:05")], ["Meet", "2026-11-19 00:05"], "already past today: tomorrow");113  assert.equal(due("Meet at 14:30"), "2026-11-18 14:30"); assert.equal(due("Meet 9am"), "2026-11-19 09:00"); assert.equal(due("Meet 12am tomorrow"), "2026-11-19 00:00"); assert.equal(due("Meet 12pm tomorrow"), "2026-11-19 12:00");114  assert.deepEqual([p("Call Marc tomorrow at 9").title, due("Call Marc tomorrow at 9")], ["Call Marc", "2026-11-19 09:00"]);115  assert.deepEqual([p("Call Marc fri at 2:30 pm").title, due("Call Marc fri at 2:30 pm")], ["Call Marc", "2026-11-20 14:30"], "the time is removed first, so the date still closes the text");116  assert.deepEqual([p("Call Marc tomorrow at 99").title, due("Call Marc tomorrow at 99")], ["Call Marc at 99", "2026-11-19 17:00"]);117});118119test("quick-add keeps priority, project and the documented examples working", () => {120  const a = p("Call Marc tomorrow !high #thesis"); assert.deepEqual([a.title, fmt(a.dueAt), a.priority, a.project], ["Call Marc", "2026-11-19 17:00", "high", "thesis"]);121  const b = p("Send abstract to JIMF nov 30 !high #thesis"); assert.deepEqual([b.title, fmt(b.dueAt), b.priority, b.project], ["Send abstract to JIMF", "2026-11-30 17:00", "high", "thesis"]);122  const c = p("Buy sun cream p2 #home"); assert.deepEqual([c.title, c.dueAt, c.priority, c.project], ["Buy sun cream", null, "medium", "home"]);123  assert.equal(p("Review !!").priority, "medium"); assert.equal(p("Review !!!").priority, "high"); assert.equal(p("Review !").priority, "low"); assert.equal(p("Review !med").priority, "medium");124  assert.equal(p("Replace p10 filter").priority, null); assert.equal(p("Replace p10 filter").title, "Replace p10 filter");125  assert.equal(p("   ").title, ""); assert.equal(p("   ").dueAt, null);126});127128test("U10: ?offset= is a plain bounded integer; absent means 0", () => {129  assert.equal(parseOffset(null), 0); assert.equal(parseOffset(undefined), 0); assert.equal(parseOffset(""), 0); assert.equal(parseOffset("0"), 0); assert.equal(parseOffset("100"), 100); assert.equal(parseOffset(String(MAX_OFFSET)), MAX_OFFSET);130  for (const bad of ["-1", "1.5", "1e3", "abc", " 5", "5 ", "0x10", String(MAX_OFFSET + 1), "9999999", "١٢"]) assert.equal(parseOffset(bad), null, bad);131});132133test("U10: the next offset is the number of rows loaded, until a page comes back short", () => {134  const full = Array.from({ length: LIST_PAGE_SIZE }, (_v, i) => ({ id: `a${i}` })); const short = [{ id: "z" }];135  assert.equal(nextOffset([]), undefined); assert.equal(nextOffset([[]]), undefined); assert.equal(nextOffset([short]), undefined);136  assert.equal(nextOffset([full]), LIST_PAGE_SIZE); assert.equal(nextOffset([full, full]), 2 * LIST_PAGE_SIZE); assert.equal(nextOffset([full, short]), undefined);137  assert.equal(nextOffset([[1, 2], [3, 4]], 2), 4); assert.equal(nextOffset([[1, 2], [3]], 2), undefined);138  assert.deepEqual(flattenPages([[{ id: "a" }, { id: "b" }], [{ id: "b" }, { id: "c" }]]).map((r) => r.id), ["a", "b", "c"], "a row shifted into the next page by an insertion is listed once");139  assert.deepEqual(flattenPages(undefined), []);140});141142test("U10: search asks for a higher limit until the cap, then says the list is truncated", () => {143  assert.deepEqual(searchPaging(12, SEARCH_PAGE_SIZE), { more: false, capped: false, nextLimit: 80 });144  assert.deepEqual(searchPaging(40, SEARCH_PAGE_SIZE), { more: true, capped: false, nextLimit: 80 });145  assert.deepEqual(searchPaging(80, 80), { more: true, capped: false, nextLimit: 120 });146  assert.deepEqual(searchPaging(97, SEARCH_MAX_LIMIT), { more: false, capped: false, nextLimit: SEARCH_MAX_LIMIT });147  assert.deepEqual(searchPaging(SEARCH_MAX_LIMIT, SEARCH_MAX_LIMIT), { more: false, capped: true, nextLimit: SEARCH_MAX_LIMIT });148  assert.equal(SEARCH_MAX_LIMIT % SEARCH_PAGE_SIZE, 0);149});150
Files main
Menu

Theme