Merge nemesis/demo 9b9b495d (live) into dev
All checks were successful
Rat dev preview / Build dev preview (push) Successful in 3m49s
All checks were successful
Rat dev preview / Build dev preview (push) Successful in 3m49s
This commit is contained in:
commit
9641cfb4f4
7 changed files with 20 additions and 223 deletions
|
|
@ -3,10 +3,7 @@ import catalogue from './catalogue.json';
|
|||
import {
|
||||
mindOfSender,
|
||||
poseSrc,
|
||||
poseStatusLine,
|
||||
poseStatuses,
|
||||
resolvePose,
|
||||
splitNewestStatus,
|
||||
} from './poses';
|
||||
|
||||
describe('resolvePose', () => {
|
||||
|
|
@ -53,52 +50,5 @@ describe('poseSrc', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('poseStatuses', () => {
|
||||
const ev = (sender: string, ts: number, label: unknown) => ({
|
||||
sender,
|
||||
localpart: sender,
|
||||
ts,
|
||||
label,
|
||||
});
|
||||
it('reports the latest pose per Mind and when its unbroken run began', () => {
|
||||
const out = poseStatuses([
|
||||
ev('aqua', 1, 'happy'),
|
||||
ev('aqua', 2, 'sigh'),
|
||||
ev('aqua', 3, 'sigh'),
|
||||
ev('alice', 4, 'happy'),
|
||||
ev('eureka', 5, 'probe'),
|
||||
]);
|
||||
expect(out).toEqual([
|
||||
{ mind: 'aqua', pose: 'sigh', since: 2 },
|
||||
{ mind: 'eureka', pose: 'probe', since: 5 },
|
||||
]);
|
||||
});
|
||||
it('treats unknown labels as neutral runs', () => {
|
||||
expect(poseStatuses([ev('void', 1, 'bogus'), ev('void', 2, undefined)])).toEqual([
|
||||
{ mind: 'void', pose: 'neutral', since: 1 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('poseStatusLine', () => {
|
||||
it('formats the client-status line', () => {
|
||||
const since = new Date(2026, 10, 4, 9, 5).getTime();
|
||||
expect(poseStatusLine({ mind: 'void', pose: 'reading', since })).toBe(
|
||||
'Ratatoskr: Void is reading, since 09:05'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('splitNewestStatus', () => {
|
||||
const s = (mind: string, since: number) => ({ mind, pose: 'happy', since });
|
||||
|
||||
it('picks the status that changed last and keeps the others in their order', () => {
|
||||
const { newest, rest } = splitNewestStatus([s('eureka', 100), s('void', 300), s('nibu', 200)]);
|
||||
expect(newest?.mind).toBe('void');
|
||||
expect(rest.map((r) => r.mind)).toEqual(['eureka', 'nibu']);
|
||||
});
|
||||
|
||||
it('has no newest for an empty list', () => {
|
||||
expect(splitNewestStatus([])).toEqual({ newest: undefined, rest: [] });
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -40,50 +40,3 @@ export const resolvePose = (mind: string, label: unknown): string => {
|
|||
export const poseSrc = (mind: string, pose: string, base = '/'): string =>
|
||||
`${base}public/poses/${artPath(mind, pose) ?? artPath(mind, NEUTRAL)}`;
|
||||
|
||||
export type PoseStatus = { mind: string; pose: string; since: number };
|
||||
export type PoseEvent = {
|
||||
sender: string;
|
||||
localpart?: string;
|
||||
name?: string;
|
||||
ts: number;
|
||||
label: unknown;
|
||||
};
|
||||
|
||||
// Latest pose per Mind in a room's events (oldest first), and when that pose
|
||||
// began: the first of its unbroken run of messages carrying the same pose.
|
||||
export const poseStatuses = (events: PoseEvent[]): PoseStatus[] => {
|
||||
const bySender = new Map<string, { mind: string; run: { pose: string; ts: number }[] }>();
|
||||
events.forEach((e) => {
|
||||
const mind = mindOfSender(e.localpart, e.name);
|
||||
if (!mind) return;
|
||||
const entry = bySender.get(e.sender) ?? { mind, run: [] };
|
||||
entry.run.push({ pose: resolvePose(mind, e.label), ts: e.ts });
|
||||
bySender.set(e.sender, entry);
|
||||
});
|
||||
return [...bySender.values()].map(({ mind, run }) => {
|
||||
const last = run[run.length - 1];
|
||||
let i = run.length - 1;
|
||||
while (i > 0 && run[i - 1].pose === last.pose) i -= 1;
|
||||
return { mind, pose: last.pose, since: run[i].ts };
|
||||
});
|
||||
};
|
||||
|
||||
// The status that changed last, and the rest in their given order (phones show only the first).
|
||||
export const splitNewestStatus = (
|
||||
statuses: PoseStatus[]
|
||||
): { newest: PoseStatus | undefined; rest: PoseStatus[] } => {
|
||||
let newest: PoseStatus | undefined;
|
||||
statuses.forEach((st) => {
|
||||
if (!newest || st.since > newest.since) newest = st;
|
||||
});
|
||||
return { newest, rest: statuses.filter((st) => st !== newest) };
|
||||
};
|
||||
|
||||
const two = (n: number) => String(n).padStart(2, '0');
|
||||
|
||||
// Client-status text. Rendered locally only; it is never sent to a room.
|
||||
export const poseStatusLine = ({ mind, pose, since }: PoseStatus): string => {
|
||||
const d = new Date(since);
|
||||
const name = mind.charAt(0).toUpperCase() + mind.slice(1);
|
||||
return `Ratatoskr: ${name} is ${pose}, since ${two(d.getHours())}:${two(d.getMinutes())}`;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue