// A refresh reopens the chat the person was in, per account; "New chat" // forgets it; a chat deleted elsewhere is simply gone. const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const src = fs.readFileSync(path.join(__dirname, '..', 'public/js/clinicalAssistant.js'), 'utf8'); test('the open chat is remembered when it is loaded or saved, and forgotten on a new chat', () => { assert.match(src, /var OPEN_CHAT_KEY = 'ped_assistant_open_chat'/); assert.match(src, /modelStorageKey\(OPEN_CHAT_KEY\)/, 'scoped to the account like the model choice'); const load = src.slice(src.indexOf('function loadSavedChat'), src.indexOf('function restoreSavedChat')); assert.match(load, /rememberOpenChat\(currentChatId\)/); const save = src.slice(src.indexOf('function performAutosave'), src.indexOf('function performAutosave') + 900); assert.match(save, /if \(data\.id\) currentChatId = data\.id;\s*\n\s*rememberOpenChat\(currentChatId\)/); const clear = src.slice(src.indexOf('function performClearConversation'), src.indexOf('function cancelAssistantSearch')); assert.match(clear, /currentChatId = null;\s*\n\s*rememberOpenChat\(null\)/); }); test('the page reopens it on load, quietly, and forgets a chat that no longer exists', () => { const init = src.slice(src.indexOf('function initIfNeeded'), src.indexOf('function onceOnDocument')); assert.match(init, /loadExamples\(\);\s*\n\s*restoreOpenChat\(\);/); const restore = src.slice(src.indexOf('function restoreOpenChat'), src.indexOf('function restoreOpenChat') + 900); assert.match(restore, /fetchSavedAssistantChat\(id\)/); assert.match(restore, /\.catch\(function \(\) \{ rememberOpenChat\(null\); \}\)/); assert.doesNotMatch(restore, /showToast/, 'a missing chat is not an error to announce'); });