Files
nous-portal/tests/AGENTS.md
T
AlexKucera b9065d409c fix: address round-2 review findings
- apply(): only swap the current model when a Nous model was filtered
  out; a session on another provider is left alone, and the notice names
  the model actually in use instead of the first catalog hit.
- Device-code poll floors the interval at 1s and treats a non-numeric
  interval as absent, matching pi-ai's own poller; interval: 0 from the
  server no longer becomes a tight loop.
- Drop the provider's own context.publish: createProvider already
  persists and publishes the fetchModels return value with checkedAt;
  the extra publish was a second, timestamp-less write each refresh.
- tokenRequest tolerates a literal null error body (data.error would
  have thrown TypeError, masking the HTTP status).
- tsconfig: remove allowImportingTsExtensions and baseUrl, both inert.
- .editorconfig covers .mjs.
- Tests: 14 (was 8). tokenRequest is exported and its HTML/null error
  paths are pinned; the no-credential test now sets NOUS_API_KEY so it
  fails if an env fallback returns; the slash commands are driven
  through a recording pi (switch/notice semantics, and persist-before-
  mutate on a failed flag write). Stub returns a private mkdtemp dir.
- CHANGELOG: /nous-catalog entry states the switch semantics.
2026-11-25 10:09:06 +01:00

2.7 KiB

tests/AGENTS.md

Purpose

Offline unit tests for index.ts. They pin the pure logic that has real branching: model mapping, credential construction, model fetching, token-endpoint error handling, and the slash commands (driven through a recording pi). No network, no real credentials.

Ownership

  • This doc owns the test suite, its loader setup, and the host-package stub.
  • Root AGENTS.md owns npm run check (which runs these tests) and repo-wide rules.
  • index.ts is owned by the root; tests import its exports but never edit it.

Local Contracts

  • index.test.mjs loads index.ts through jiti, the same TS loader pi uses, because the host packages are not installed in node_modules.
  • Host imports are aliased to stubs/pi-ai.mjs. Alias keys are matched longest-prefix first: @earendil-works/pi-ai/compat must be listed before @earendil-works/pi-ai, or the bare key swallows the subpath and resolution fails.
  • stubs/pi-ai.mjs exports only what index.ts imports: envApiKeyAuth, createProvider, openAICompletionsApi, getAgentDir (plus agentDir so tests can find the flag file). getAgentDir is called inside the default export, so it is reached only by tests that drive the commands; it returns a private mkdtemp dir per run, shared through globalThis because jiti loads the stub as a separate instance from a native import. It is a type-less stub; correctness of those functions is pi's concern and is verified live, not here.
  • Exported functions under test: toModel, toCredential, tokenRequest, fetchModels, tierOf, withoutTierHeader, and the default export via the drive() harness. Anything else is not unit-tested (notably the device-code poll loop inside login, which would need timer control and extraction).
  • Network calls are replaced with t.mock.method(globalThis, "fetch", …) from node:test, which restores itself when the test ends. Do not hand-roll a globalThis.fetch swap.
  • fetchModels reads a key only from the credential argument. Env resolution of $NOUS_API_KEY is envApiKeyAuth's job upstream. The one test that touches process.env sets it deliberately, to pin that fetchModels ignores it; it restores the previous value in t.after.

Work Guidance

  • Test behaviour, not implementation. Each test pins one contract from the function under test.
  • When adding an export with branching logic to index.ts, add a test here in the same change.
  • Keep the stub minimal: add an export only when index.ts starts importing it at module load.

Verification

  • npm test (or npm run check) runs node --test tests/*.test.mjs. All tests must pass.

Child DOX Index

Path Scope
(none) stubs/ holds a single file governed by the contracts above