2026-06-30 14:43:33 -04:00
|
|
|
import { test } from 'node:test';
|
|
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
|
import { createStore } from 'jotai';
|
2026-07-03 22:30:57 -04:00
|
|
|
import { toastQueueAtom, dismissToastAtom, ToastNotif, createDownloadToast } from './toast';
|
2026-06-30 14:43:33 -04:00
|
|
|
|
|
|
|
|
// The queue lives in an unexported baseAtom; we drive the two write-only setters
|
|
|
|
|
// (toastQueueAtom append + null no-op guard, dismissToastAtom remove-by-id)
|
|
|
|
|
// through a jotai store and read back via toastQueueAtom's getter.
|
|
|
|
|
|
|
|
|
|
const makeToast = (id: string): ToastNotif => ({
|
|
|
|
|
id,
|
|
|
|
|
displayName: `name-${id}`,
|
|
|
|
|
body: `body-${id}`,
|
|
|
|
|
roomName: `room-${id}`,
|
|
|
|
|
roomId: `!${id}:server`,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('starts empty', () => {
|
|
|
|
|
const store = createStore();
|
|
|
|
|
assert.deepEqual(store.get(toastQueueAtom), []);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('toastQueueAtom appends in order', () => {
|
|
|
|
|
const store = createStore();
|
|
|
|
|
const a = makeToast('a');
|
|
|
|
|
const b = makeToast('b');
|
|
|
|
|
store.set(toastQueueAtom, a);
|
|
|
|
|
store.set(toastQueueAtom, b);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
|
|
|
|
['a', 'b'],
|
|
|
|
|
);
|
|
|
|
|
assert.equal(store.get(toastQueueAtom)[0], a);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('toastQueueAtom ignores null (no-op guard)', () => {
|
|
|
|
|
const store = createStore();
|
|
|
|
|
store.set(toastQueueAtom, makeToast('a'));
|
|
|
|
|
store.set(toastQueueAtom, null);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
|
|
|
|
['a'],
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('toastQueueAtom allows duplicate ids (no dedupe)', () => {
|
|
|
|
|
const store = createStore();
|
|
|
|
|
store.set(toastQueueAtom, makeToast('a'));
|
|
|
|
|
store.set(toastQueueAtom, makeToast('a'));
|
|
|
|
|
assert.equal(store.get(toastQueueAtom).length, 2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('dismissToastAtom removes the matching id only', () => {
|
|
|
|
|
const store = createStore();
|
|
|
|
|
store.set(toastQueueAtom, makeToast('a'));
|
|
|
|
|
store.set(toastQueueAtom, makeToast('b'));
|
|
|
|
|
store.set(toastQueueAtom, makeToast('c'));
|
|
|
|
|
|
|
|
|
|
store.set(dismissToastAtom, 'b');
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
|
|
|
|
['a', 'c'],
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('dismissToastAtom removes every entry sharing the id', () => {
|
|
|
|
|
const store = createStore();
|
|
|
|
|
store.set(toastQueueAtom, makeToast('a'));
|
|
|
|
|
store.set(toastQueueAtom, makeToast('a'));
|
|
|
|
|
store.set(toastQueueAtom, makeToast('b'));
|
|
|
|
|
|
|
|
|
|
store.set(dismissToastAtom, 'a');
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
|
|
|
|
['b'],
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('dismissToastAtom for an unknown id is a no-op', () => {
|
|
|
|
|
const store = createStore();
|
|
|
|
|
store.set(toastQueueAtom, makeToast('a'));
|
|
|
|
|
store.set(dismissToastAtom, 'missing');
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
|
|
|
|
['a'],
|
|
|
|
|
);
|
|
|
|
|
});
|
2026-07-03 22:30:57 -04:00
|
|
|
|
|
|
|
|
test('createDownloadToast: filename in body, no room navigation, unique ids', () => {
|
|
|
|
|
const a = createDownloadToast('photo.jpg');
|
|
|
|
|
assert.equal(a.displayName, 'Downloaded');
|
|
|
|
|
assert.equal(a.body, 'photo.jpg');
|
|
|
|
|
// roomId empty + an onClick present → clicking dismisses without navigating to a room.
|
|
|
|
|
assert.equal(a.roomId, '');
|
|
|
|
|
assert.equal(a.roomName, '');
|
|
|
|
|
assert.equal(typeof a.onClick, 'function');
|
|
|
|
|
const b = createDownloadToast('photo.jpg');
|
|
|
|
|
assert.notEqual(a.id, b.id);
|
|
|
|
|
});
|