import test from "node:test"; import assert from "node:assert/strict"; import { extractTexAnnotationFromHtml, normalizeSelectedText, wrapLatexSource, } from "../lib/selection-text"; import { quoteAttachment, quoteText } from "../lib/quote-attachments"; import { responderFromPreferences, sideChatTurnConfig } from "../lib/side-chat"; test("normalizes selected chat text without flattening paragraphs", () => { assert.equal( normalizeSelectedText(" first line\r\n\r\n\r\n second\tline "), "first line\n\nsecond line", ); }); test("wrapLatexSource matches Markdown math delimiters", () => { assert.equal(wrapLatexSource("a^2", false), "$a^2$"); assert.equal( wrapLatexSource("I(\\theta) = a^2", true), "$$I(\\theta) = a^2$$", ); }); test("KaTeX annotation remaps to math that grounds in Markdown source", () => { const tex = "I(\\theta) = a^2 \\cdot P(\\theta) \\cdot (1 - P(\\theta))"; const html = `${tex}I(θ)=a²`; assert.equal(extractTexAnnotationFromHtml(html), tex); const source = `$$${tex}$$`; const selected = wrapLatexSource( extractTexAnnotationFromHtml(html) ?? "", true, ); assert.equal(selected, source); assert.ok(source.includes(selected)); }); test("a quote travels as UTF-8 text and reads back unchanged", () => { const quote = quoteAttachment(" 父进程中的 rc 是子进程 PID\n$x^2$ "); assert.ok(quote); assert.equal(quote.type, "quote"); assert.equal(quote.text, "父进程中的 rc 是子进程 PID\n$x^2$"); assert.equal(quoteText({ base64: quote.base64 }), quote.text); assert.equal(quoteAttachment(" "), null); }); test("a side chat names its parent and who answers it", () => { const base = { parentSessionId: "parent-1", leafMessageId: 9 }; assert.deepEqual( sideChatTurnConfig({ ...base, responder: { kind: "dito" } }), { selection_tutor_context: { parent_session_id: "parent-1", source_message_id: 9, quoted_message_id: null, }, answer_by: "", }, ); assert.equal( sideChatTurnConfig({ ...base, responder: { kind: "partner", id: "ada", name: "Ada" }, }).answer_by, "partner:ada", ); assert.deepEqual( sideChatTurnConfig({ ...base, responder: { kind: "partner_group", id: "g1", name: "Study group" }, }), { selection_tutor_context: { parent_session_id: "parent-1", source_message_id: 9, quoted_message_id: null, }, answer_by: "", partner_discussion_group_id: "g1", }, ); }); test("a side chat records the message its quote came from", () => { const base = { parentSessionId: "parent-1", leafMessageId: 9, responder: { kind: "dito" as const }, }; assert.deepEqual( sideChatTurnConfig({ ...base, sourceMessageId: 4 }), { selection_tutor_context: { parent_session_id: "parent-1", source_message_id: 9, quoted_message_id: 4, }, answer_by: "", }, ); // PocketBase ids are opaque strings; they must survive untouched. assert.deepEqual( sideChatTurnConfig({ ...base, sourceMessageId: "pb_msg_42" }), { selection_tutor_context: { parent_session_id: "parent-1", source_message_id: 9, quoted_message_id: "pb_msg_42", }, answer_by: "", }, ); }); test("a reopened side chat keeps answering through whoever answered it", () => { assert.deepEqual( responderFromPreferences({ answer_by: "subagent:my-codex" }), { kind: "subagent", id: "my-codex", name: "my-codex", }, ); assert.deepEqual( responderFromPreferences({ side_chat_group_id: "g1", answer_by: "" }), { kind: "partner_group", id: "g1", name: "g1", }, ); assert.deepEqual(responderFromPreferences({}), { kind: "dito" }); });