import test from 'node:test';
import assert from 'node:assert/strict';
import { renderToStaticMarkup } from 'react-dom/server';
import { AutomaticActionNotice } from '../components/automatic-action-notice';
import { advanceActionNotices, appendActionNotices, type AutomaticActionEvent } from '../lib/action-notice-queue';
const events = (first: number, count: number): AutomaticActionEvent[] =>
Array.from({ length: count }, (_, index) => ({
id: `room:${first + index}`, seq: first + index,
faction: index % 2 ? 'emperor' : 'atreides', name: `Action ${first + index}`,
}));
void test('ordinary notices keep their public action and house without mutating inputs', () => {
const incoming = events(10, 3), before = structuredClone(incoming);
assert.deepEqual(appendActionNotices([], incoming, false), incoming.map(event => ({ ...event, count: 1 })));
assert.deepEqual(incoming, before);
assert.deepEqual(appendActionNotices([], [], false), []);
});
void test('a full log batch becomes three brief notices while accounting for every action', () => {
const incoming = events(1, 250);
const queue = appendActionNotices([], incoming, false);
assert.equal(queue.length, 3);
assert.deepEqual(queue.map(notice => notice.seq), [248, 249, 250]);
assert.deepEqual(queue.map(notice => notice.count), [248, 1, 1]);
assert.equal(queue.reduce((sum, notice) => sum + notice.count, 0), 250);
assert.equal(queue[0].name, incoming[247].name);
assert.equal(queue[0].faction, incoming[247].faction);
});
void test('incoming bursts preserve the visible notice and merge existing summaries without losing counts', () => {
const queue = appendActionNotices([], events(1, 12), false);
const active = queue[0], before = structuredClone(queue);
const next = appendActionNotices(queue.slice(1), events(13, 20), true);
assert.equal(next.length, 2);
assert.deepEqual(next.map(notice => notice.seq), [31, 32]);
assert.equal(active.count + next.reduce((sum, notice) => sum + notice.count, 0), 32);
const again = appendActionNotices(next, events(33, 2), true);
assert.deepEqual(again.map(notice => notice.seq), [33, 34]);
assert.equal(active.count + again.reduce((sum, notice) => sum + notice.count, 0), 34);
assert.deepEqual(queue, before);
});
void test('the initial render establishes a silent baseline and leaves a nonblocking accessible output', () => {
const markup = renderToStaticMarkup();
assert.match(markup, /aria-label="Automatic action notices"/);
assert.match(markup, /aria-pressed="true"/);
assert.match(markup, /