Files
poli0981andClaude Opus 5 8f121a8e9c feat(worker): review queue, admin API, ingest and suggest endpoints (v4 Phase 4)
Adds the server side of the admin and the automatic game discovery. The
catalog stays JSON in this repo; the Worker writes it as a GitHub App
(GraphQL createCommitOnBranch with expectedHeadOid: verified commits,
retried on a race) and keeps only the review queue in D1.

- worker/admin.ts: /api/admin/* (queue list, approve/reject, add, edit the
  three maintainer fields, remove with reason + R2 cover cleanup, restore)
  and the /admin shell with its own CSP. Every request re-verifies the
  Cloudflare Access JWT (aud + maintainer email + same-origin writes).
  Approving commits to scripts/temp_link.json, which starts update.yml.
  The maintainer's email goes to the D1 audit log only, never a commit.
- worker/ingest.ts: POST /api/ingest for the browser extension (Access
  service token, per-client rate limit, Idempotency-Key replay, <= 150
  items / 64 KB), replacing its PAT commits.
- worker/suggest.ts: public POST /api/suggest (same-origin, per-IP rate
  limit, Turnstile siteverify, canonical itch.io URL) into the queue.
- worker/discover.ts + scheduled(): every 4 h, one itch.io RSS feed
  (round-robin, 6 h back-off on 429) into the queue, then reconcile
  (queued -> ingested / failed) and retention (idempotency 7 d, form
  notes 180 d).
- worker/queue.ts: URL classification (catalog, pending, deleted,
  blocklist) and the D1 store; worker/repo.ts builds each admin commit
  from the files at the head of main, byte-compatible with the Python
  pipeline (golden fixtures).
- wrangler.jsonc: D1 (bound by name; migrations applied by hand), rate
  limits, cron, non-secret settings as vars (empty until setup: features
  answer 503 until configured), staging env without the cron.
  `npm run cf:types` ignores .dev.vars so the committed types are stable.
- Tests: 159 passing; the SQL runs against a real local D1 (workerd via
  wrangler's platform proxy).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:55:01 +07:00

58 lines
2.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { classify, type ClassifyContext } from './queue'
const ctx = (over: Partial<ClassifyContext> = {}): ClassifyContext => ({
catalog: new Set(['https://dev.itch.io/in-catalog']),
deleted: new Map([['https://dev.itch.io/gone', { reason: 'paid', deleted_at: '2026-06-19T03:00:00Z' }]]),
known: new Map([
['https://dev.itch.io/waiting', 'pending'],
['https://dev.itch.io/approved', 'queued'],
['https://dev.itch.io/blocked', 'rejected'],
['https://dev.itch.io/broke', 'failed'],
]),
...over,
})
describe('classify', () => {
it('assigns each outcome in precedence order', () => {
const out = classify(
[
'not a url',
'https://dev.itch.io/new',
'HTTPS://DEV.itch.io/new/', // same game after canonicalization
'https://dev.itch.io/in-catalog',
'https://dev.itch.io/blocked',
'https://dev.itch.io/waiting',
'https://dev.itch.io/approved',
'https://dev.itch.io/gone',
'https://dev.itch.io/broke',
],
ctx(),
)
expect(out.map((c) => [c.outcome, c.store])).toEqual([
['invalid_url', false],
['queued', true],
['duplicate_in_request', false],
['duplicate_catalog', false],
['rejected', false],
['duplicate_pending', false],
['duplicate_pending', false],
['previously_deleted', true],
['queued', true], // failed rows are reopened
])
expect(out[1].canonical).toBe('https://dev.itch.io/new')
expect(out[7].reason).toBe('paid (2026-06-19)')
})
it('checks the catalog before the blocklist and the deleted log', () => {
const url = 'https://dev.itch.io/in-catalog'
const out = classify([url], ctx({ known: new Map([[url, 'rejected']]), deleted: new Map() }))
expect(out[0].outcome).toBe('duplicate_catalog')
})
it('keeps the caller input verbatim', () => {
const [c] = classify([' https://dev.itch.io/new?ref=x '], ctx())
expect(c.input).toBe(' https://dev.itch.io/new?ref=x ')
})
})