Split monolithic chat service into modular structure with Matrix client support, extract web server into per-resource route modules, introduce bootstrap/main-loop separation, and add comprehensive vitest coverage across services, repositories, and utilities. Adds project docs and CLAUDE.md agent instructions.
2.7 KiB
RedditBot — agent instructions
Test-first workflow (MANDATORY)
This applies to every code-changing task in this repo. Sub-agents inherit it.
1. Before implementation — establish the baseline
Run the full test suite first:
npm test
Note the count of passing / failing tests. Do not start implementing until you have this baseline. If something is already failing, surface it to the user before touching it — don't silently inherit pre-existing failures into your scope.
2. During implementation — update tests alongside code
- When you change a function's behavior, update or add the test for it in the same change.
- When you change a function's signature, fix the call sites in tests in the same change.
- When you introduce a new module, add a test for it in
tests/<name>.test.ts. - If a test fails because the test is wrong (stale assertion, ESM mocking issue, real env leaking in), fix the test. If it fails because the code is wrong, fix the code.
- Never delete a test to make it pass. Either fix the assertion to reflect intended behavior, or fix the code.
3. Before reporting done — confirm green
Re-run the full suite:
npm test
npx tsc --noEmit
Both must be clean before you say the task is done. If either is failing, either fix it or explicitly call out the regression in your final message — do not report success and leave a broken suite.
If tests pass count went down vs. the baseline you took in step 1, that is a regression. Treat it as a blocker.
Why this matters
The test suite is the only automated safety net for this project (no CI yet, no integration tests). Drift here means real bugs ship. Past incident: a refactor went out with 129 pre-existing test failures undetected — they had been broken for weeks because nobody re-ran the suite.
Project notes for agents
- Runtime: TypeScript with
module: NodeNext. Imports use.jsextensions even for.tsfiles. - Tests: vitest,
globals: falsesodescribe/it/expect/beforeEach/afterEachmust be imported explicitly. - The OpenRouter, MediaService, ProcessorService, RateLimiter, and MonitorService constructors accept optional last parameters for test-injectable dependencies (mock clients, temp dirs, fake fetchers, shortened timing). Use them rather than
vi.mock()of ESM modules — the namespace-mocking pattern fails under NodeNext. - Shadow-DOM scraping for
chat.reddit.comlives insrc/services/chat/scrapers.tsas typedpage.evaluatefunctions. ThemyUsernameparameter is threaded through fromconfig.reddit.username. - Some
.envvars (notablyOPENROUTER_API_KEY) are auto-loaded viadotenv.config()at module import. Tests that touchloadConfigmust snapshot+clear these inbeforeEachand restore inafterEach.