| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { 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("a conversation is shown while any surface shows it", () => { |
| 22 | const panel = showConversation("chn_dm"); |
| 23 | const card = showConversation("chn_dm"); |
| 24 | assert.ok(shownConversations().has("chn_dm")); |
| 25 | panel(); |
| 26 | panel(); |
| 27 | assert.ok(shownConversations().has("chn_dm"), "the card still shows it; letting go twice counts once"); |
| 28 | card(); |
| 29 | assert.ok(!shownConversations().has("chn_dm")); |
| 30 | }); |