feat: serve the BYOS device API
The panel now pairs, fetches its image and files its logs against this application rather than against the TRMNL cloud. Four endpoints: /api/setup issues a token on first contact, /api/display hands back an image and a wake interval, /api/log stores firmware diagnostics, and /api/device/image/<hash> serves the bytes. The wake interval is where freshness and battery are traded off. In BYOS nothing can be pushed: the device sleeps, wakes, asks and sleeps again. So the interval is short while the shop trades and long overnight, and it is shortened further whenever a change of state falls inside it — the door opening in twenty minutes means waking in twenty-one, whatever the base interval says. The image filename is the hash of its own bytes. The firmware skips the redraw when the name is unchanged, which is the whole battery strategy, and the URL is immutable, unguessable and safe to cache forever. Two integration tests pin this: unchanged data must yield the same filename and store one row, changed hours must yield a different one. MAC addresses are normalised before use. They are a primary key here, and firmwares are inconsistent about case and separators; without this a panel could register twice by capitalising itself differently. Header names are read in both the hyphen and underscore spellings for the same reason — the TRMNL docs and the Seeed sources disagree, and being liberal costs nothing while being wrong costs a blank shop window. Pairing is deliberately made to survive a rendering failure. The token is issued once and only its digest is kept, so a device stranded by a failed response would be registered yet hold no credential, and unable to register again. The welcome image is worth far less than that. This was found by running the flow, not by reading it. satori, yoga and harfbuzz are marked external: bundling rewrites the relative path satori uses to load its WebAssembly, and the renderer dies on a missing hb.wasm. The integration tests run against a real Postgres, in CI too. Mocking Prisma here would only prove the mock works. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import type { ResolvedDay, ShopStatus, ShopStatusKind } from '@/lib/schedule/types';
|
||||
|
||||
import {
|
||||
CHANGE_MARGIN_SEC,
|
||||
MAX_REFRESH_SEC,
|
||||
MIN_REFRESH_SEC,
|
||||
computeRefreshRate,
|
||||
} from './refresh';
|
||||
|
||||
const ZURICH = 'Europe/Zurich';
|
||||
const OPEN_SEC = 600;
|
||||
const CLOSED_SEC = 7200;
|
||||
|
||||
const DAY: ResolvedDay = {
|
||||
date: '2026-09-22',
|
||||
dayOfWeek: 2,
|
||||
isOpen: true,
|
||||
slots: [],
|
||||
isException: false,
|
||||
exceptionKind: null,
|
||||
noteFr: null,
|
||||
noteEn: null,
|
||||
};
|
||||
|
||||
function status(
|
||||
kind: ShopStatusKind,
|
||||
nextChangeAt: ShopStatus['nextChangeAt'] = null,
|
||||
): ShopStatus {
|
||||
return { status: kind, today: DAY, nextChangeAt, nextOpening: nextChangeAt };
|
||||
}
|
||||
|
||||
function rate(now: string, value: ShopStatus): number {
|
||||
return computeRefreshRate({
|
||||
now: new Date(now),
|
||||
status: value,
|
||||
timezone: ZURICH,
|
||||
openSec: OPEN_SEC,
|
||||
closedSec: CLOSED_SEC,
|
||||
});
|
||||
}
|
||||
|
||||
describe('computeRefreshRate', () => {
|
||||
it('uses the short interval while the shop is trading', () => {
|
||||
// 12:00 local, closing at 18:30: far from any change, so the base wins.
|
||||
expect(rate('2026-09-22T10:00:00Z', status('OPEN', { date: '2026-09-22', time: '18:30' }))).toBe(
|
||||
OPEN_SEC,
|
||||
);
|
||||
});
|
||||
|
||||
it('uses the long interval overnight', () => {
|
||||
expect(
|
||||
rate('2026-09-22T21:00:00Z', status('CLOSED', { date: '2026-09-23', time: '10:00' })),
|
||||
).toBe(CLOSED_SEC);
|
||||
});
|
||||
|
||||
it('treats opening soon as a trading moment', () => {
|
||||
// Someone is standing at the window right now; the screen must not be stale.
|
||||
expect(
|
||||
rate('2026-09-22T07:40:00Z', status('OPENING_SOON', { date: '2026-09-22', time: '10:00' })),
|
||||
).toBeLessThanOrEqual(OPEN_SEC);
|
||||
});
|
||||
|
||||
it('never sleeps through a change of state', () => {
|
||||
// 17:00 local, closing at 18:30 is 90 minutes away, well inside the long
|
||||
// interval. The device must still wake just after the change.
|
||||
const seconds = rate(
|
||||
'2026-09-22T15:00:00Z',
|
||||
status('CLOSED', { date: '2026-09-22', time: '18:30' }),
|
||||
);
|
||||
expect(seconds).toBe(Math.min(CLOSED_SEC, 90 * 60 + CHANGE_MARGIN_SEC));
|
||||
});
|
||||
|
||||
it('wakes just after the change rather than exactly on it', () => {
|
||||
// Closing in ten minutes: waking at the stroke of 18:30 risks redrawing
|
||||
// the old state, so add the margin.
|
||||
const seconds = rate(
|
||||
'2026-09-22T16:20:00Z',
|
||||
status('OPEN', { date: '2026-09-22', time: '18:30' }),
|
||||
);
|
||||
expect(seconds).toBe(130 * 60 > OPEN_SEC ? OPEN_SEC : 130 * 60 + CHANGE_MARGIN_SEC);
|
||||
});
|
||||
|
||||
it('shortens the long interval for a change that falls inside it', () => {
|
||||
// Closed, reopening in 30 minutes, base interval 2 hours.
|
||||
const seconds = rate(
|
||||
'2026-09-22T07:30:00Z',
|
||||
status('CLOSED', { date: '2026-09-22', time: '10:00' }),
|
||||
);
|
||||
expect(seconds).toBe(30 * 60 + CHANGE_MARGIN_SEC);
|
||||
});
|
||||
|
||||
it('falls back to the base interval when nothing is scheduled ahead', () => {
|
||||
expect(rate('2026-09-22T10:00:00Z', status('CLOSED', null))).toBe(CLOSED_SEC);
|
||||
});
|
||||
|
||||
it('ignores a change that has already passed', () => {
|
||||
// A stale next-change must not produce a negative or zero interval.
|
||||
expect(
|
||||
rate('2026-09-22T16:00:00Z', status('CLOSED', { date: '2026-09-22', time: '10:00' })),
|
||||
).toBe(CLOSED_SEC);
|
||||
});
|
||||
|
||||
it('never returns less than the floor', () => {
|
||||
const seconds = rate(
|
||||
'2026-09-22T10:00:00Z',
|
||||
status('OPEN', { date: '2026-09-22', time: '12:00' }),
|
||||
);
|
||||
expect(seconds).toBeGreaterThanOrEqual(MIN_REFRESH_SEC);
|
||||
});
|
||||
|
||||
it('never returns more than the ceiling', () => {
|
||||
expect(
|
||||
computeRefreshRate({
|
||||
now: new Date('2026-09-22T10:00:00Z'),
|
||||
status: status('CLOSED', null),
|
||||
timezone: ZURICH,
|
||||
openSec: OPEN_SEC,
|
||||
closedSec: 999_999,
|
||||
}),
|
||||
).toBe(MAX_REFRESH_SEC);
|
||||
});
|
||||
|
||||
it('handles a change several days out without overflowing', () => {
|
||||
expect(
|
||||
rate('2026-09-26T17:00:00Z', status('CLOSED', { date: '2026-09-29', time: '10:00' })),
|
||||
).toBe(CLOSED_SEC);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user