|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { nextUnreviewed, wrapNextTarget, wrapPrevTarget } from "./seek.js"; |
| 4 | + |
| 5 | +// A finished predicate backed by a set of signed-off file indices. A file absent from the set |
| 6 | +// is unreviewed — this is how a hash-invalidated file (signed off, then edited by the agent) |
| 7 | +// re-enters the seek: the store's fileFinished returns false for it, so it's just "not in set". |
| 8 | +const finishedIn = (done: number[]) => { |
| 9 | + const s = new Set(done); |
| 10 | + return (i: number) => s.has(i); |
| 11 | +}; |
| 12 | + |
| 13 | +test("nextUnreviewed wraps past the end to the first unreviewed file — guide order", () => { |
| 14 | + // Guide order is not the file-array order; the seek must follow it. |
| 15 | + const order = [3, 1, 4, 0, 2]; |
| 16 | + // Everything signed off except file index 1; cur is the last file in the order (2). |
| 17 | + const next = nextUnreviewed(order, 2, finishedIn([3, 4, 0, 2])); |
| 18 | + assert.equal(next, 1, "scans forward from the last slot, wraps, lands on 1"); |
| 19 | +}); |
| 20 | + |
| 21 | +test("nextUnreviewed wraps past the end to the first unreviewed file — file order", () => { |
| 22 | + const order = [0, 1, 2, 3, 4]; |
| 23 | + // Only file 2 remains; cur is the last file (4). |
| 24 | + assert.equal(nextUnreviewed(order, 4, finishedIn([0, 1, 3, 4])), 2); |
| 25 | +}); |
| 26 | + |
| 27 | +test("nextUnreviewed returns null when the current file is the last remaining one", () => { |
| 28 | + // Approve-advance on the final unreviewed file: after approving, cur is finished and every |
| 29 | + // other file already was — no wrap target, so the caller shows the review-complete prompt. |
| 30 | + const order = [0, 1, 2]; |
| 31 | + assert.equal(nextUnreviewed(order, 1, finishedIn([0, 1, 2])), null); |
| 32 | +}); |
| 33 | + |
| 34 | +test("nextUnreviewed never returns the current file, even when it is unreviewed", () => { |
| 35 | + // The just-approved file is finished in practice, but guard the wrap regardless. |
| 36 | + const order = [0, 1, 2]; |
| 37 | + assert.equal(nextUnreviewed(order, 1, finishedIn([0, 2])), null); |
| 38 | +}); |
| 39 | + |
| 40 | +test("nextUnreviewed treats a hash-invalidated (dropped-out) file as a wrap target", () => { |
| 41 | + // File 0 was signed off then invalidated by an agent edit → back to unreviewed. |
| 42 | + const order = [0, 1, 2]; |
| 43 | + assert.equal(nextUnreviewed(order, 2, finishedIn([1, 2])), 0); |
| 44 | +}); |
| 45 | + |
| 46 | +test("nextUnreviewed starts from the top when cur is not in the order", () => { |
| 47 | + // A diff file absent from the guide order isn't in `order`; scan from the first slot. |
| 48 | + const order = [3, 1, 4]; |
| 49 | + assert.equal(nextUnreviewed(order, 99, finishedIn([3])), 1); |
| 50 | +}); |
| 51 | + |
| 52 | +test("partial guide: approve-advance and wrap reach the unlisted 'Other' files", () => { |
| 53 | + // navOrder() builds guide order followed by changed files the guide didn't list. Here the |
| 54 | + // guide covers files 2 and 0; files 1 and 3 are unlisted, appended in file-array order. |
| 55 | + const order = [2, 0, 1, 3]; |
| 56 | + // Approve the last guide-listed file (0) while an unlisted file (1) is still pending → |
| 57 | + // advance must land on it, not dead-end at the end of the guided sequence. |
| 58 | + assert.equal(nextUnreviewed(order, 0, finishedIn([2, 0])), 1); |
| 59 | + // The plain-next wrap likewise targets an unlisted pending file. |
| 60 | + assert.equal(wrapNextTarget(order, finishedIn([2, 0])), 1); |
| 61 | + assert.equal(wrapPrevTarget(order, finishedIn([2, 0])), 3); |
| 62 | +}); |
| 63 | + |
| 64 | +test("wrapNextTarget lands on the first unreviewed file when work remains", () => { |
| 65 | + assert.equal(wrapNextTarget([3, 1, 4, 0, 2], finishedIn([3, 1])), 4, "guide order"); |
| 66 | + assert.equal(wrapNextTarget([0, 1, 2, 3], finishedIn([0])), 1, "file order"); |
| 67 | +}); |
| 68 | + |
| 69 | +test("wrapNextTarget cycles to the first file when everything is reviewed", () => { |
| 70 | + assert.equal(wrapNextTarget([3, 1, 4], finishedIn([3, 1, 4])), 3); |
| 71 | +}); |
| 72 | + |
| 73 | +test("wrapPrevTarget lands on the last unreviewed file when work remains", () => { |
| 74 | + assert.equal(wrapPrevTarget([3, 1, 4, 0, 2], finishedIn([2, 0])), 4, "guide order"); |
| 75 | + assert.equal(wrapPrevTarget([0, 1, 2, 3], finishedIn([3])), 2, "file order"); |
| 76 | +}); |
| 77 | + |
| 78 | +test("wrapPrevTarget cycles to the last file when everything is reviewed", () => { |
| 79 | + assert.equal(wrapPrevTarget([3, 1, 4], finishedIn([3, 1, 4])), 4); |
| 80 | +}); |
| 81 | + |
| 82 | +test("empty order yields no target", () => { |
| 83 | + const none = finishedIn([]); |
| 84 | + assert.equal(nextUnreviewed([], 0, none), null); |
| 85 | + assert.equal(wrapNextTarget([], none), null); |
| 86 | + assert.equal(wrapPrevTarget([], none), null); |
| 87 | +}); |
0 commit comments