Files
client/test/media.test.mjs
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

136 lines
5.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { env } from './fixtures/renderer-env.mjs';
import { avatarUrl, iconUrl, channelBannerUrl, serverBannerUrl, pruneMedia, clearMedia } from '../app/renderer/js/media.js';
import { S, newConnection } from '../app/renderer/js/state.js';
const conn = newConnection('c1');
S.connections.set(conn.id, conn);
S.viewId = conn.id;
const live = new Set();
let id = 0;
URL.createObjectURL = () => { const url = `blob:fixture-${++id}`; live.add(url); return url; };
URL.revokeObjectURL = url => live.delete(url);
const image = { mime: 'image/png', data: Buffer.from('generated image bytes').toString('base64') };
const settle = async () => { await Promise.resolve(); await Promise.resolve(); };
const snapshot = clients => ({ server: {}, channels: [], clients });
test('snapshot pruning releases replaced/departed avatars without evicting current images', async () => {
env.respond = async () => ({ ok: true, result: image });
const old = { id: 1, uid: 'one', avatar_hash: 'old' };
const current = { ...old, avatar_hash: 'current' };
const oldUrl = await avatarUrl(old);
pruneMedia(snapshot([current]), conn);
await settle();
assert.equal(live.has(oldUrl), false);
const currentUrl = await avatarUrl(current);
pruneMedia(snapshot([current]), conn);
assert.equal(await avatarUrl(current), currentUrl);
assert.equal(live.size, 1);
pruneMedia(snapshot([]), conn);
await settle();
assert.equal(live.size, 0);
});
test('current channel/group icons and banners survive pruning even when not visible', async () => {
const channel = { id: 1, icon_id: 1234, banner_url: 'ts3image://fixture' };
const server = { hostbanner_gfx_url: 'https://example.invalid/banner' };
const urls = await Promise.all([iconUrl(1234), iconUrl(5678), channelBannerUrl(channel), serverBannerUrl(server)]);
pruneMedia({ channels: [channel], server, clients: [], server_groups: [{ icon_id: 5678 }] }, conn);
await settle();
for (const url of urls) assert.equal(live.has(url), true);
clearMedia(conn);
await settle();
assert.equal(live.size, 0);
});
test('late image completion is revoked after its owner leaves, not leaked or reused', async () => {
let complete;
env.respond = () => new Promise(resolve => { complete = resolve; });
const client = { id: 5, uid: 'late', avatar_hash: 'pending' };
const loading = avatarUrl(client);
pruneMedia(snapshot([]), conn);
complete({ ok: true, result: image });
const staleUrl = await loading;
await settle();
assert.equal(live.has(staleUrl), false);
env.respond = async () => ({ ok: true, result: image });
const newUrl = await avatarUrl(client);
assert.notEqual(newUrl, staleUrl);
assert.equal(live.has(newUrl), true);
clearMedia(conn);
await settle();
});
test('A-B-A pending avatar completion cannot expose an obsolete URL to a reused element', async () => {
const replies = [];
env.respond = () => new Promise(resolve => replies.push(resolve));
const client = { id: 5, uid: 'same-client', avatar_hash: 'A' };
const old = avatarUrl(client);
pruneMedia(snapshot([{ ...client, avatar_hash: 'B' }]), conn);
const current = avatarUrl(client);
pruneMedia(snapshot([client]), conn);
replies[1]({ ok: true, result: image });
const currentUrl = await current;
replies[0]({ ok: true, result: image });
assert.equal(await old, null);
assert.equal(await avatarUrl(client), currentUrl);
assert.deepEqual([...live], [currentUrl]);
clearMedia(conn);
await settle();
});
test('server reset invalidates pending icon completion but unrelated snapshots keep current requests', async () => {
const replies = [];
env.respond = () => new Promise(resolve => replies.push(resolve));
const old = iconUrl(1234);
clearMedia(conn);
const current = iconUrl(1234);
pruneMedia({ channels: [{ icon_id: 1234 }], clients: [], server: {} }, conn);
replies[0]({ ok: true, result: image });
assert.equal(await old, null);
replies[1]({ ok: true, result: image });
const url = await current;
assert.ok(live.has(url));
assert.equal(await iconUrl(1234), url);
clearMedia(conn);
await settle();
});
test('stale failed request cannot replace a new cache entry of the same key', async () => {
const replies = [];
env.respond = () => new Promise(resolve => replies.push(resolve));
const old = iconUrl(1234);
clearMedia(conn);
const current = iconUrl(1234);
replies[1]({ ok: true, result: image });
const url = await current;
replies[0]({ ok: false, error: { code: 'not_found', message: 'old server' } });
assert.equal(await old, null);
assert.equal(await iconUrl(1234), url);
assert.deepEqual([...live], [url]);
clearMedia(conn);
await settle();
});
test('same media IDs on simultaneous servers never share cache entries or request targets', async () => {
const other = newConnection('c2');
S.connections.set(other.id, other);
env.requests.length = 0;
env.respond = async () => ({ ok: true, result: image });
const first = await iconUrl(1234, conn);
const second = await iconUrl(1234, other);
assert.notEqual(first, second);
assert.deepEqual(env.requests.map((request) => request.connectionId), ['c1', 'c2']);
clearMedia(conn);
await settle();
assert.equal(live.has(first), false);
assert.equal(live.has(second), true);
assert.equal(await iconUrl(1234, other), second);
clearMedia(other);
S.connections.delete(other.id);
await settle();
assert.equal(live.size, 0);
});