| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { newestFinished, readNow, showConversation, shownConversations } from "./read-when-shown.ts"; |
| 5 | |
| 6 | const moment = { visible: true, onScreen: true, newestId: "msg_2", lastRead: null }; |
| 7 | |
| 8 | test("messages are read only while the page is visible and they are on screen", () => { |
| 9 | assert.equal(readNow(moment), "msg_2"); |
| 10 | assert.equal(readNow({ ...moment, visible: false }), null, "a hidden tab reads nothing"); |
| 11 | assert.equal(readNow({ ...moment, onScreen: false }), null, "the panel closed, the card scrolled away"); |
| 12 | assert.equal(readNow({ ...moment, newestId: null }), null, "nothing there yet"); |
| 13 | }); |
| 14 | |
| 15 | test("each newest id is read once, and only forward", () => { |
| 16 | assert.equal(readNow({ ...moment, lastRead: "msg_2" }), null); |
| 17 | assert.equal(readNow({ ...moment, lastRead: "msg_3" }), null); |
| 18 | assert.equal(readNow({ ...moment, newestId: "msg_3", lastRead: "msg_2" }), "msg_3"); |
| 19 | }); |
| 20 | |
| 21 | test("an answer is read once it is finished, not while it streams", () => { |
| 22 | const question = { id: "msg_1", answer: null, deleted_at: null }; |
| 23 | const answer = (state: "thinking" | "streaming" | "done") => ({ id: "msg_2", answer: { state } as never, deleted_at: null }); |
| 24 | assert.equal(newestFinished([question, answer("thinking")]), "msg_1"); |
| 25 | assert.equal(newestFinished([question, answer("streaming")]), "msg_1"); |
| 26 | assert.equal(newestFinished([question, answer("done")]), "msg_2"); |
| 27 | assert.equal(newestFinished([]), null); |
| 28 | assert.equal(newestFinished([{ ...question, pending: true }]), null, "a question still being sent has no id to read"); |
| 29 | assert.equal(newestFinished([question, { id: "msg_3", answer: null, deleted_at: "2026-10-20T00:00:00Z" }]), "msg_1"); |
| 30 | }); |
| 31 | |
| 32 | test("a conversation is shown while any surface shows it", () => { |
| 33 | const panel = showConversation("chn_dm"); |
| 34 | const card = showConversation("chn_dm"); |
| 35 | assert.ok(shownConversations().has("chn_dm")); |
| 36 | panel(); |
| 37 | panel(); |
| 38 | assert.ok(shownConversations().has("chn_dm"), "the card still shows it; letting go twice counts once"); |
| 39 | card(); |
| 40 | assert.ok(!shownConversations().has("chn_dm")); |
| 41 | }); |