Files
gacha-event-tracker/scripts/build-feed.ts
T
Lucas WintherandClaude Opus 5 1156c358a7 fix: stop failing the build on a page that says it has no events
Infinity Nikki's wiki prints "There are no Events in this category"
between versions, and the refresh runner has honoured that since
2026-09-03 — it stores the empty parse as the source's real answer
rather than letting a quiet lane reach the broken tier. The feed build
never asked, so the same page arrived at CI as parsedCount 0,
indistinguishable from a parser that has stopped reading a redesigned
page, and brokenSources failed every build while every refresh stayed
green.

The runner's verdict cannot travel on its own: only a parser has seen
the page, and by the time brokenSources runs there is nothing left but
the feed. So the fact rides on SourceHealth, defaulted so an older feed
the service worker cached keeps validating and reads as the strict
answer.

Both ends now ask it the same way — of an empty parse only, from the
page's own words only — because a redesign yields zero rows too, and
excusing that is the silently emptied calendar the gate exists for.

The rule sits in a module rather than in build-feed.ts, which writes
public/ and so runs a build if a test imports it. That is how the two
ends drifted apart with nothing to catch it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-09-06 18:02:16 +02:00

128 lines
4.5 KiB
TypeScript

/**
* Build the static event feed from cached snapshots, falling back to fixtures.
*
* Offline: this reads files on disk, never the network. Fetching is
* `scripts/refresh-sources.ts`'s job; this stage only parses what that left in
* the snapshot cache. On a clean checkout — and in the container build — no
* snapshot exists and the checked-in fixture is used instead, so the build
* stays reproducible and a wiki being down never breaks it.
*
* bun run build:feed
*/
import { ADAPTERS } from "../src/ingest/adapters/index.ts";
import { sourceHealth } from "../src/ingest/health.ts";
import { mergeEvents } from "../src/ingest/merge.ts";
import { SnapshotStore, freshnessAt } from "../src/ingest/snapshots.ts";
import { EventFeed, SCHEMA_VERSION, type SourceHealth } from "../src/shared/feed.ts";
import type { GachaEvent, GameId } from "../src/shared/schema.ts";
const OUT = "public/data/events.v1.json";
const snapshots = new SnapshotStore(process.env["SNAPSHOT_DIR"] ?? "snapshots");
/**
* Newest fixture for one *source*, not one game.
*
* A game can have several sources, and fixtures are named `<site>-events-<date>`
* against adapter ids of `<game>-<site>-events`. Globbing by game alone hands
* one site's page to another site's parser.
*/
async function latestFixture(adapterId: string, game: GameId) {
const site = adapterId.replace(`${game}-`, "").replace(/-events$/, "");
const pattern = `fixtures/${game}/${site}-*.html`;
const files = [...new Bun.Glob(pattern).scanSync(".")].sort();
const file = files.at(-1);
if (file === undefined) {
throw new Error(`no fixture found for ${adapterId} (${pattern})`);
}
return { file, html: await Bun.file(file).text() };
}
/**
* The document to parse for one source: the live snapshot when the refresh
* runner has cached one, otherwise the newest checked-in fixture.
*
* `at` is what the UI's staleness badge reads, so it must never claim to be
* fresher than the bytes actually are — a fixture reports its capture date.
*/
async function documentFor(adapterId: string, game: GameId) {
const cached = await snapshots.read(adapterId);
if (cached !== null) {
return {
file: snapshots.bodyPath(adapterId),
html: cached.html,
at: freshnessAt(cached),
};
}
const { file, html } = await latestFixture(adapterId, game);
return { file, html, at: fixtureDate(file) };
}
const now = new Date().toISOString();
const byGame = new Map<GameId, GachaEvent[][]>();
const sources: SourceHealth[] = [];
for (const adapter of ADAPTERS) {
const { file, html, at } = await documentFor(adapter.id, adapter.game);
const events = adapter.parse(html, {
now,
sourceUrl: adapter.url,
sourceId: adapter.id,
game: adapter.game,
});
// Which of the three empties this is, decided in a module a test can reach
// rather than here — see `src/ingest/health.ts` for why that matters.
const health = sourceHealth(adapter, html, at, events.length);
const { parsedCount } = health;
const groups = byGame.get(adapter.game) ?? [];
groups.push(events);
byGame.set(adapter.game, groups);
sources.push(health);
// A source that came back with nothing says which nothing it was, because a
// bare "0 events" reads as a fault and two of the three are not one.
const note = health.statesNoEvents
? " (the page states it currently lists none)"
: events.length === 0 && parsedCount !== null && parsedCount > 0
? ` (all ${parsedCount} have ended — stale page)`
: "";
console.log(
` ${adapter.id.padEnd(24)} ${String(events.length).padStart(3)} events ← ${file}${note}`,
);
}
const events: GachaEvent[] = [];
let conflictCount = 0;
for (const [, groups] of byGame) {
const merged = mergeEvents(groups);
events.push(...merged.events);
conflictCount += merged.conflicts.length;
for (const c of merged.conflicts) {
console.warn(
` ! conflict: "${c.kept.title}" ${c.field} differs by ${c.deltaHours}h between sources`,
);
}
}
events.sort((a, b) => a.startsAt.localeCompare(b.startsAt) || a.id.localeCompare(b.id));
const feed = EventFeed.parse({
schemaVersion: SCHEMA_VERSION,
generatedAt: now,
events,
sources,
});
await Bun.write(OUT, `${JSON.stringify(feed, null, 2)}\n`);
console.log(
`\n${OUT}: ${events.length} events across ${byGame.size} games, ${conflictCount} conflicts`,
);
/** "game8-events-2026-08-14.html" → ISO timestamp. */
function fixtureDate(path: string): string | null {
const m = /(\d{4}-\d{2}-\d{2})\.html$/.exec(path);
return m?.[1] ? `${m[1]}T00:00:00.000Z` : null;
}