Files
client/test/chat-main.test.js
T
omar e6464bc4ed feat: checkpoint account-free client workflows and source-matched UI
Preserve accumulated client implementation since v0.1.4, including local identities, multi-server state, chat, administration, audio and streaming controls. Refine official-source layout, original artwork, responsive composer overflow, settings navigation and chat scroll anchoring.

Current verification: Node suite passes; isolated packaged chat and responsive control smokes pass without device capture. Native core is reused, not rebuilt in this pass. Strict whole-screen visual parity remains incomplete; no 1:1 or cross-platform acceptance claim. Local checkpoint only; no release or push.
2026-11-01 19:43:18 +03:00

49 lines
3.4 KiB
JavaScript

'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const { ChatHistoryStore, validScope, validRecord, validExternalUrl } = require('../app/main/chat-main');
const base = { identity: 'IdentityA=', server: 'ServerA=' };
const scope = { ...base, kind: 'client', key: 'PartnerA=' };
const record = (i) => ({ time: 1700000000000 + i, type: 'text', from_uid: 'PartnerA=', from_name: '<script>sender</script>', own: false, text: `Message ${i} **bold**` });
test('concurrent initial appends serialize; pages remain isolated and durable across store reopen', async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'chat-unit-'));
try {
const store = new ChatHistoryStore(dir);
await Promise.all(Array.from({ length: 641 }, (_, i) => store.append(scope, record(i), 'Partner')));
await store.setOpen(scope, true, 'Partner');
const reopened = new ChatHistoryStore(dir);
const latest = await reopened.page(scope, -1, 200);
assert.equal(latest.total, 641); assert.equal(latest.messages.length, 200);
assert.deepEqual(latest.messages.map((m) => m.index), Array.from({ length: 200 }, (_, i) => i + 441));
let before = -1; const indices = new Set();
do { const page = await reopened.page(scope, before, 100); for (const message of page.messages) indices.add(message.index); before = page.start; } while (before > 0);
assert.equal(indices.size, 641);
for (const other of [{ ...scope, identity: 'IdentityB=' }, { ...scope, server: 'ServerB=' }, { ...scope, key: 'PartnerB=' }, { ...base, kind: 'server', key: 'server' }]) assert.equal((await reopened.page(other, -1, 100)).total, 0);
assert.equal((await reopened.list(base)).conversations[0].open, true);
assert.deepEqual(await reopened.list({ ...base, identity: 'IdentityB=' }), { conversations: [] });
assert.equal(reopened.conversations.get(reopened.conversationDir(scope)).messages, undefined, 'store caches counters, not history rows');
} finally { await fs.rm(dir, { recursive: true, force: true }); }
});
test('interrupted trailing append is repaired without corrupting page indices', async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'chat-repair-'));
try {
const store = new ChatHistoryStore(dir); await store.append(scope, record(0));
await fs.appendFile(path.join(store.conversationDir(scope), '000000.jsonl'), '{"t":1');
const reopened = new ChatHistoryStore(dir); await reopened.append(scope, record(1));
assert.equal((await reopened.page(scope, -1, 100)).messages.length, 2);
} finally { await fs.rm(dir, { recursive: true, force: true }); }
});
test('history validation and external URL allowlist reject malformed and unsafe inputs', () => {
for (const identity of ['../other', '', 'x\n']) assert.throws(() => validScope({ ...scope, identity }));
for (const key of ['../other', '0', '-1']) assert.throws(() => validScope({ ...base, kind: 'channel', key }));
for (const text of [null, 'x'.repeat(32769)]) assert.throws(() => validRecord({ ...record(0), text }));
for (const url of ['javascript:alert(1)', 'file:///C:/private', 'data:text/html,<script>', '//example.com', 'https://example.com\n']) assert.throws(() => validExternalUrl(url));
for (const url of ['https://example.com/', 'http://example.com/', 'mailto:person@example.com']) assert.equal(validExternalUrl(url), url);
});