Files
splatoon3.ink/app/data/updaters/DataUpdater.test.mjs
Matt Isenhower aa71bf2eda
Some checks failed
Build frontend / build (24.x) (push) Has been cancelled
Lint / lint (push) Has been cancelled
Tests / test (24.x) (push) Has been cancelled
Hold a lane run while an earlier one may still be going; log data changes
Cloudflare replaced the fast lane's Durable Object during the 00:00 UTC run.
The alarm restarted on a new instance while the old one carried on posting,
and the old run's results were lost because it could no longer write to its
own storage. Like the old cron, a run now takes a lock in the private R2
bucket (which a replaced instance can still write), refreshes it every 10
seconds and deletes it at the end; a run that finds a fresh lock waits and
checks again in 15 seconds. A scheduled run that never saved its results is
recorded as cut off.

Each updater now reports whether its published file changed, in its log
line and in the run's results, to see when fresh data really arrives at the
top of the hour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 16:23:40 -08:00

170 lines
6.0 KiB
JavaScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import DataUpdater from './DataUpdater.mjs';
import { storage } from '../../common/storage/index.mjs';
import { testBuckets } from '../../test/storage.mjs';
import RunLimits from '../RunLimits.mjs';
describe('DataUpdater', () => {
describe('deriveIds', () => {
it('replaces every recursively matched node while preserving its data', () => {
const updater = Object.create(DataUpdater.prototype);
updater.derivedIds = ['$..weapons.*'];
const splattershot = {
name: 'Splattershot',
image: { url: 'https://example.com/assets/weapon-a.png' },
};
const splatRoller = {
name: 'Splat Roller',
image: { url: 'https://example.com/assets/weapon-b.png' },
};
const scheduleWeapons = [splattershot];
const nestedWeapons = [splatRoller];
const data = {
schedules: [
{
weapons: scheduleWeapons,
},
{
nested: {
weapons: nestedWeapons,
},
},
],
};
updater.deriveIds(data);
const weapons = [...scheduleWeapons, ...nestedWeapons];
expect(weapons.map(({ name }) => name)).toEqual(['Splattershot', 'Splat Roller']);
expect(weapons.map(({ __splatoon3ink_id: id }) => id)).toEqual([
'1bfe00916f1f6b9a',
'027798dcfb87596b',
]);
});
});
describe('storage', () => {
function makeUpdater(props = {}) {
return Object.assign(Object.create(DataUpdater.prototype), {
filename: 'gear',
outputDirectory: 'data',
archiveOnePerHour: true,
limits: new RunLimits,
...props,
});
}
afterEach(() => {
vi.unstubAllEnvs();
vi.useRealTimers();
vi.restoreAllMocks();
});
it('publishes data to public storage', async () => {
await makeUpdater().saveData({ data: { a: 1 } });
expect(testBuckets().public.json('data/gear.json')).toEqual({ data: { a: 1 } });
expect(testBuckets().archive.objects.size).toBe(0);
});
it('reports whether the published data changed', async () => {
expect(await makeUpdater().saveData({ data: { a: 1 } })).toBe(true);
expect(await makeUpdater().saveData({ data: { a: 1 } })).toBe(false);
expect(await makeUpdater().saveData({ data: { a: 2 } })).toBe(true);
});
it('publishes data in a subdirectory', async () => {
await makeUpdater({ directory: 'xrank', filename: 'xrank.detail.a-1.splatzones' }).saveData({});
expect(testBuckets().public.json('data/xrank/xrank.detail.a-1.splatzones.json')).toEqual({});
});
it('writes an hourly archive snapshot when ARCHIVE_DATA is set', async () => {
vi.stubEnv('ARCHIVE_DATA', 'true');
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-11-08T12:34:56Z'));
await makeUpdater().saveData({ data: { a: 1 } });
expect([...testBuckets().archive.objects.keys()]).toEqual(['2026-11-08.12-00-00.gear.json']);
});
it('writes no snapshot when ARCHIVE_DATA is false', async () => {
vi.stubEnv('ARCHIVE_DATA', 'false');
await makeUpdater().saveData({ data: { a: 1 } });
expect(testBuckets().archive.objects.size).toBe(0);
});
it('stamps a snapshot to the second when it is not hourly', async () => {
vi.stubEnv('ARCHIVE_DATA', 'true');
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-11-08T12:34:56Z'));
await makeUpdater({ archiveOnePerHour: false, filename: 'xrank' }).saveData({});
expect([...testBuckets().archive.objects.keys()]).toEqual(['2026-11-08.12-34-56.xrank.json']);
});
it('still publishes when the snapshot cannot be archived', async () => {
vi.stubEnv('ARCHIVE_DATA', 'true');
let warn = vi.fn();
let writeText = vi.spyOn(storage().archive, 'writeText').mockRejectedValue(new Error('Archive upload failed with HTTP 503'));
await makeUpdater({ _console: { warn } }).saveData({ data: { a: 1 } });
expect(writeText).toHaveBeenCalledOnce();
expect(testBuckets().public.json('data/gear.json')).toEqual({ data: { a: 1 } });
expect(warn).toHaveBeenCalledWith('Could not archive gear:', expect.objectContaining({ message: 'Archive upload failed with HTTP 503' }));
});
it('holds the snapshot for later when the run defers archiving', async () => {
vi.stubEnv('ARCHIVE_DATA', 'true');
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-11-08T12:59:58Z'));
let limits = new RunLimits({ deferArchive: true });
await makeUpdater({ limits }).saveData({ data: { a: 1 } });
expect(testBuckets().public.json('data/gear.json')).toEqual({ data: { a: 1 } });
expect(testBuckets().archive.objects.size).toBe(0);
expect([...limits.heldSnapshots.values()]).toEqual([
{ key: '2026-11-08.12-00-00.gear.json', text: JSON.stringify({ data: { a: 1 } }), name: 'gear' },
]);
});
it('publishes calendars next to the data', async () => {
let updater = makeUpdater({
calendarName: 'Test',
getCalendarEntries: () => [{
id: 'fest-1',
title: 'Splatfest',
url: 'https://splatoon3.ink',
imageUrl: 'https://assets.test/a.png',
start: '2026-11-08T00:00:00Z',
end: '2026-11-09T00:00:00Z',
}],
});
await updater.updateCalendarEvents({});
expect(testBuckets().public.text('data/gear.ics')).toContain('BEGIN:VCALENDAR');
expect(testBuckets().public.objects.get('data/gear.ics').httpMetadata.contentType).toBe('text/calendar');
});
it('treats missing or stale published files as older than a cutoff', async () => {
let updater = makeUpdater();
let cutoff = new Date(Date.now() + 60_000);
expect(await updater.olderThan('data/gear.json', cutoff)).toBe(true);
await updater.saveData({});
expect(await updater.olderThan('data/gear.json', cutoff)).toBe(true);
expect(await updater.olderThan('data/gear.json', new Date(Date.now() - 60_000))).toBe(false);
});
});
});