import { createRequire } from "module"; const require = createRequire("/tmp/claude-0/npmwork/"); globalThis.ExcelJS = require("exceljs"); import assert from "node:assert/strict"; import fs from "node:fs"; import * as W from "../static/js/workbook.js"; import * as C from "../static/js/core.js"; import * as R from "../static/js/rag.js"; const mk = (no, date, wt, pr, id) => ({ entryNo: no, date, workType: wt, product: pr, title: "T " + id, itemId: id, description: "d", loggedAt: "" }); const mail = JSON.parse(fs.readFileSync("static/seeds/mail_seeds.json")).map(s => R.newTemplate(s.title, s.body, s.tags, s.lang || "")); const st = { owner: "a@indusface.com", mail, scripts: [], entries: [ mk(1, "2026-09-29", "Ticket", "WAF", "T-1"), mk(2, "2026-09-30", "POC", "WAS", "P-1"), mk(3, "2026-10-01", "Ticket", "WAS", "T-2"), mk(4, "2026-10-05", "Ticket", "WAF", "T-3"), mk(5, "2026-10-06", "POC", "WAF", "P-2")] }; let buf = await W.newWorkbookBuffer(st); fs.writeFileSync("/tmp/claude-0/wb_test.xlsx", Buffer.from(buf)); let wb = await W.load(buf); console.log("sheets:", wb.worksheets.map(w => w.name).join(", ")); let back = W.readState(wb); assert.equal(back.entries.length, 5); assert.equal(back.owner, "a@indusface.com"); assert.equal(back.mail.length, mail.length); assert.deepEqual(back.entries.map(e => e.date), st.entries.map(e => e.date)); // update flow: add + move month + preserve foreign sheet wb.addWorksheet("My Notes").addRow(["keep me"]); buf = await wb.xlsx.writeBuffer(); const out = await W.updateBuffer(buf, s => { s.entries.push(mk(W.nextEntryNo(s.entries), "2026-11-02", "Ticket", "WAF", "T-9")); s.entries[0].date = "2026-08-31"; }); wb = await W.load(out.buffer); back = W.readState(wb); console.log("sheets:", wb.worksheets.map(w => w.name).join(", ")); assert.ok(wb.getWorksheet("My Notes")); assert.ok(wb.getWorksheet("2026-08")); assert.ok(wb.getWorksheet("2026-11")); assert.equal(back.entries.length, 6); // hand-typed row with no entry no gets a number const ws = wb.getWorksheet("2026-10"); ws.addRow([null, new Date(Date.UTC(2026, 9, 8)), null, "ticket", "waf", "Manual", "T-77", "typed in Excel"]); back = W.readState(wb); assert.ok(back.entries.find(e => e.itemId === "T-77").entryNo > 0); // bad date aborts instead of silently dropping ws.addRow([null, "not a date", null, "ticket", "waf", "x", "T-78"]); assert.throws(() => W.readState(wb), /date is not valid/); // engine assert.equal(C.isoWeek(C.parseISO("2026-10-09")), "2026-W41"); assert.equal(C.isoWeek(C.parseISO("2026-12-31")), "2026-W53"); assert.equal(C.isoWeek(C.parseISO("2027-01-01")), "2026-W53"); const today = C.parseISO("2026-10-10"), rows = st.entries; const r1 = C.execute(C.parseQuestion("average tickets per week this month", today), rows, today); console.log(r1.interpreted, "|", r1.summary); const r2 = C.execute(C.parseQuestion("how many WAF last month", today), rows, today); console.log(r2.interpreted, "|", r2.summary); assert.equal(C.parseQuestion("weekly report waf", today).intent, "group_week"); // rag const hit = R.search(mail, "need a follow-up mail, no response from customer", 2); assert.match(hit[0].t.title, /Follow-up/); const f = R.fill(mail[0].body, { ticket_id: "TKT-5", customer_name: "Rahul" }); assert.ok(f.text.includes("TKT-5") && f.missing.includes("issue_title")); console.log("OK");