| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { projectSessionRows, sessionKey } from "./session-order.mjs"; |
| 4 | |
| 5 | const sessions = [ |
| 6 | { id: "one", title: "第一段", pinned: true }, |
| 7 | { id: "two", title: "第二段", active: true }, |
| 8 | { id: "three", title: "第三段" }, |
| 9 | ]; |
| 10 | const keys = rows => rows.map(sessionKey); |
| 11 | test("external running state blocks only history without an owned live terminal", () => { |
| 12 | const running = [{id:"external",running:true}, {id:"owned",running:true}, {id:"stopped",running:false}]; |
| 13 | const tabs = [{id:"terminal:owned",cli_session_id:"owned"}, {id:"terminal:old",cli_session_id:"external",closed:true}]; |
| 14 | const rows = projectSessionRows(tabs, running, {}, undefined, new Map()); |
| 15 | assert.equal(rows.find(row=>row.cli_session_id==="external").sessionRunningElsewhere,true); |
| 16 | assert.equal(rows.find(row=>row.cli_session_id==="owned").sessionRunningElsewhere,false); |
| 17 | assert.equal(rows.find(row=>row.cli_session_id==="stopped").sessionRunningElsewhere,false); |
| 18 | const exited = projectSessionRows(tabs, running.map(session=>({...session,running:false})), {}, undefined, new Map()); |
| 19 | assert.equal(exited.some(row=>row.sessionRunningElsewhere),false); |
| 20 | }); |
| 21 | test("default activity, stable clicks and terminal attachment", () => { |
| 22 | const baseline = new Map(); |
| 23 | const initial = projectSessionRows([], sessions, {}, undefined, baseline); |
| 24 | assert.deepEqual(keys(initial), ["session:two", "session:one", "session:three"]); |
| 25 | const open = [{ id: "terminal:new", cli_session_id: "three", kind: "terminal" }]; |
| 26 | assert.deepEqual(keys(projectSessionRows(open, [...sessions].reverse(), {}, undefined, baseline)), keys(initial)); |
| 27 | const ended = [{ ...open[0], closed: true }]; |
| 28 | assert.equal(projectSessionRows(ended, sessions, {}, undefined, baseline).length, 3); |
| 29 | }); |
| 30 | test("manual order overrides activity and pinning, survives terminal ID changes", () => { |
| 31 | const manual = ["session:three", "session:one", "session:two"]; |
| 32 | for (const id of ["terminal:first", "terminal:second"]) { |
| 33 | const rows = projectSessionRows([{ id, cli_session_id: "three" }], sessions, {}, manual, new Map()); |
| 34 | assert.deepEqual(keys(rows), manual); |
| 35 | } |
| 36 | const rows = projectSessionRows([], [...sessions, { id: "new", active: true }], {}, manual, new Map()); |
| 37 | assert.deepEqual(keys(rows), [...manual, "session:new"]); |
| 38 | }); |