/// import assert from "node:assert/strict"; import { test } from "node:test"; // Excluded from tsc (tsconfig.json) — never bundled/emitted, run natively via // `node --test`, which requires the literal `.ts` extension (Node's ESM // resolver does not remap `.js` specifiers to `.ts` files at runtime). import type { StatusModelEntry } from "./legal-api.ts"; import { isWritableStatus, labelFor, pickSyncTargetIssue, resolveIssueStatus, resolveStatusLabel, type SyncCandidate, } from "./sync-target.ts"; /** * הכלל הישן (worker.ts:894 לפני #446): כל issue מקושר שסטטוסו שונה מהיעד — * נכתב, בלי סינון parentId ובלי סינון-סטטוס. */ function legacyTargets( candidates: readonly SyncCandidate[], targetStatus: string, ): string[] { return candidates.filter((c) => c.status !== targetStatus).map((c) => c.id); } test("רגרסיית 8125-09-24 / CMPA-140: 11 ה-issues הממשיים של התיק, אף sub-task done לא נבחר", () => { // נמדד ב-DB החי של Paperclip (2026-08-24) — כל ה-issues המקושרים לתיק // 8125-09-24. CMPA-140 (זה שנדרס בפועל, done→in_progress) הוא **ילד** של // CMPA-139, לא שורש. CMPA-139 עצמו אינו ברשימה בכוונה: נמדד שאין לו // רשומת legal-case-number ב-plugin_state, ולכן הוא לא מגיע כמועמד כלל — // לילד יש הורה שאינו מקושר. const candidates: SyncCandidate[] = [ { id: "CMPA-116", status: "cancelled", parentId: null, companyId: "c1" }, { id: "CMPA-84", status: "cancelled", parentId: null, companyId: "c1" }, { id: "CMPA-86", status: "cancelled", parentId: null, companyId: "c1" }, { id: "CMPA-140", status: "done", parentId: "CMPA-139", companyId: "c1" }, // זה שנדרס בפועל { id: "CMPA-141", status: "cancelled", parentId: "CMPA-139", companyId: "c1", }, { id: "CMPA-85", status: "done", parentId: "CMPA-84", companyId: "c1" }, { id: "CMPA-146", status: "done", parentId: "CMPA-84", companyId: "c1" }, { id: "CMPA-147", status: "done", parentId: "CMPA-84", companyId: "c1" }, { id: "CMPA-87", status: "done", parentId: "CMPA-86", companyId: "c1" }, { id: "CMPA-88", status: "done", parentId: "CMPA-86", companyId: "c1" }, { id: "CMPA-89", status: "cancelled", parentId: "CMPA-86", companyId: "c1", }, ]; // סטטוס-התיק היה drafted → in_progress (CASE_STATUS_TO_ISSUE_STATUS ב-worker.ts). const targetStatus = "in_progress"; // הכלל הישן היה כותב על CMPA-140 — זה הבאג שתועד. assert.ok(legacyTargets(candidates, targetStatus).includes("CMPA-140")); const result = pickSyncTargetIssue(candidates); // הכלל החדש אינו כותב על דבר. assert.equal(result.target, null); assert.equal(result.reason, "no_writable_root"); }); test("sub-task יחיד ב-done ומקושר: sub-task לעולם לא נבחר", () => { const candidates: SyncCandidate[] = [ { id: "sub-1", status: "done", parentId: "some-root", companyId: "c1" }, ]; const result = pickSyncTargetIssue(candidates); assert.equal(result.target, null); assert.equal(result.reason, "no_writable_root"); }); test("sub-task יחיד פתוח (in_progress) + שורש cancelled: פתוח אינו מספיק, חייב שורש", () => { const candidates: SyncCandidate[] = [ { id: "root-1", status: "cancelled", parentId: null, companyId: "c1" }, { id: "sub-1", status: "in_progress", parentId: "root-1", companyId: "c1" }, ]; const result = pickSyncTargetIssue(candidates); assert.equal(result.target, null); assert.equal(result.reason, "no_writable_root"); }); test("שורש יחיד ב-todo: נבחר", () => { const candidates: SyncCandidate[] = [ { id: "root-1", status: "todo", parentId: null, companyId: "c1" }, ]; const result = pickSyncTargetIssue(candidates); assert.equal(result.target?.id, "root-1"); assert.equal(result.reason, "ok"); assert.equal(result.writableRoots, 1); }); // הכרעת #626 (2026-08-26): in_review כבר לא ב-NON_WRITABLE_STATUSES — // שורש יחיד ב-in_review עכשיו נבחר כיעד-כתיבה, במקום להיחסם. test("שורש יחיד ב-in_review: נבחר (הכרעת #626 — 2026-08-26)", () => { const candidates: SyncCandidate[] = [ { id: "root-1", status: "in_review", parentId: null, companyId: "c1" }, ]; const result = pickSyncTargetIssue(candidates); assert.equal(result.target?.id, "root-1"); assert.equal(result.reason, "ok"); assert.equal(result.writableRoots, 1); }); test("שורש יחיד ב-blocked: לא נכתב", () => { const candidates: SyncCandidate[] = [ { id: "root-1", status: "blocked", parentId: null, companyId: "c1" }, ]; const result = pickSyncTargetIssue(candidates); assert.equal(result.target, null); assert.equal(result.reason, "no_writable_root"); }); test("isWritableStatus: הגבול המלא בין בר-כתיבה ללא-בר-כתיבה", () => { assert.equal(isWritableStatus("in_review"), true); assert.equal(isWritableStatus("blocked"), false); assert.equal(isWritableStatus("done"), false); assert.equal(isWritableStatus("cancelled"), false); assert.equal(isWritableStatus("todo"), true); }); test("שני שורשים ברי-כתיבה: ambiguous, לא נכתב", () => { const candidates: SyncCandidate[] = [ { id: "root-1", status: "todo", parentId: null, companyId: "c1" }, { id: "root-2", status: "in_progress", parentId: null, companyId: "c1" }, ]; const result = pickSyncTargetIssue(candidates); assert.equal(result.target, null); assert.equal(result.reason, "ambiguous_writable_roots"); assert.equal(result.writableRoots, 2); }); test("שורש todo + ילד in_progress: השורש נבחר", () => { const candidates: SyncCandidate[] = [ { id: "root-1", status: "todo", parentId: null, companyId: "c1" }, { id: "child-1", status: "in_progress", parentId: "root-1", companyId: "c1", }, ]; const result = pickSyncTargetIssue(candidates); assert.equal(result.target?.id, "root-1"); assert.equal(result.reason, "ok"); }); /** * פיקסצ'ר ריאלי של `/api/status-model` (case_status_model.py) — הסדר קובע * מי הוא "todo" (האינדקס הראשון), ו-`terminal: true` קובע מי "done". */ const STATUS_MODEL: StatusModelEntry[] = [ { key: "new", label: "חדש", description: "תיק נפתח, טרם הועלו מסמכים", phase: "intake", selectable: true, terminal: false, on_enter: null, }, { key: "processing", label: "בעיבוד", description: "מסמכים בעיבוד", phase: "intake", selectable: false, terminal: false, on_enter: null, }, { key: "documents_ready", label: "מסמכים מוכנים", description: "העיבוד הושלם", phase: "intake", selectable: true, terminal: false, on_enter: null, }, { key: "analyst_verified", label: 'אומת ע"י אנליסט', description: "אנליסט אימת את התוכן", phase: "analysis", selectable: true, terminal: false, on_enter: null, }, { key: "research_complete", label: "מחקר הושלם", description: "מחקר תקדימים הושלם", phase: "analysis", selectable: true, terminal: false, on_enter: null, }, { key: "outcome_set", label: "תוצאה נקבעה", description: "תוצאה הוזנה", phase: "analysis", selectable: true, terminal: false, on_enter: null, }, { key: "direction_approved", label: "כיוון אושר", description: "כיוון הכתיבה אושר", phase: "drafting", selectable: true, terminal: false, on_enter: null, }, { key: "qa_review", label: "בדיקת איכות", description: "בבדיקת איכות", phase: "drafting", selectable: true, terminal: false, on_enter: null, }, { key: "drafted", label: "טיוטה מוכנה", description: "טיוטה נכתבה", phase: "drafting", selectable: true, terminal: false, on_enter: null, }, { key: "exported", label: "יוצא ל-DOCX", description: "יוצא כקובץ DOCX", phase: "review", selectable: true, terminal: false, on_enter: null, }, { key: "reviewed", label: 'נבדק ע"י דפנה', description: "דפנה הגיהה", phase: "review", selectable: true, terminal: false, on_enter: null, }, { key: "final", label: "סופי", description: "גרסה סופית", phase: "final", selectable: true, terminal: true, on_enter: null, }, ]; test("resolveIssueStatus: הסטטוס הראשון ברשימה (new) → todo", () => { assert.equal(resolveIssueStatus(STATUS_MODEL, "new"), "todo"); }); test("resolveIssueStatus: סטטוס terminal (final) → done", () => { assert.equal(resolveIssueStatus(STATUS_MODEL, "final"), "done"); }); test("resolveIssueStatus: analyst_verified (חסר במפה הישנה) → in_progress", () => { assert.equal( resolveIssueStatus(STATUS_MODEL, "analyst_verified"), "in_progress", ); }); test("resolveIssueStatus: research_complete → in_progress", () => { assert.equal( resolveIssueStatus(STATUS_MODEL, "research_complete"), "in_progress", ); }); test("resolveIssueStatus: סטטוס לא-מוכר → null (לא בליעה שקטה)", () => { assert.equal( resolveIssueStatus(STATUS_MODEL, "totally_unknown_status"), null, ); }); test("labelFor: מחזיר את התווית מהפיקסצ'ר, ו-null לסטטוס לא-מוכר", () => { assert.equal(labelFor(STATUS_MODEL, "new"), "חדש"); assert.equal(labelFor(STATUS_MODEL, "totally_unknown_status"), null); }); test("רגרסיה: companyId של המועמד-הנבחר שורד את pickSyncTargetIssue (לא companyId חיצוני קבוע)", () => { const candidates: SyncCandidate[] = [ { id: "issue-a", status: "cancelled", parentId: null, companyId: "company-a", }, { id: "issue-b", status: "todo", parentId: null, companyId: "company-b", }, ]; const result = pickSyncTargetIssue(candidates); assert.equal(result.target?.id, "issue-b"); assert.equal(result.target?.companyId, "company-b"); }); test("resolveStatusLabel: סטטוס מוכר → תווית מה-SSOT, בלי fallback", () => { assert.deepEqual(resolveStatusLabel(STATUS_MODEL, "new"), { label: "חדש", fallback: null, }); }); test("resolveStatusLabel: סטטוס שאינו במודל → warning-worthy fallback + ערך גולמי, בלי זריקה", () => { assert.doesNotThrow(() => resolveStatusLabel(STATUS_MODEL, "totally_unknown_status"), ); assert.deepEqual(resolveStatusLabel(STATUS_MODEL, "totally_unknown_status"), { label: "totally_unknown_status", fallback: "unknown_status", }); }); test("resolveStatusLabel: מודל לא-זמין (null) → model_unavailable, נבדל מסטטוס-לא-מוכר", () => { assert.deepEqual(resolveStatusLabel(null, "processing"), { label: "processing", fallback: "model_unavailable", }); }); test("רגרסיה #616: כל הסטטוסים שהיו במפה הקשיחה ועודם חיים מקבלים תווית מה-SSOT", () => { const survivingKeys = [ "new", "processing", "documents_ready", "outcome_set", "direction_approved", "qa_review", "drafted", "exported", "reviewed", "final", ].filter((key) => STATUS_MODEL.some((s) => s.key === key)); for (const key of survivingKeys) { const result = resolveStatusLabel(STATUS_MODEL, key); assert.equal(result.fallback, null, `${key}: expected no fallback`); assert.equal( typeof result.label, "string", `${key}: expected string label`, ); assert.notEqual(result.label, "", `${key}: expected non-empty label`); } });