- 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.
2.7 KiB
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.tsis owned by the root; tests import its exports but never edit it.
Local Contracts
index.test.mjsloadsindex.tsthrough jiti, the same TS loader pi uses, because the host packages are not installed innode_modules.- Host imports are aliased to
stubs/pi-ai.mjs. Alias keys are matched longest-prefix first:@earendil-works/pi-ai/compatmust be listed before@earendil-works/pi-ai, or the bare key swallows the subpath and resolution fails. stubs/pi-ai.mjsexports only whatindex.tsimports:envApiKeyAuth,createProvider,openAICompletionsApi,getAgentDir(plusagentDirso tests can find the flag file).getAgentDiris called inside the default export, so it is reached only by tests that drive the commands; it returns a privatemkdtempdir per run, shared throughglobalThisbecause 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 thedrive()harness. Anything else is not unit-tested (notably the device-code poll loop insidelogin, which would need timer control and extraction). - Network calls are replaced with
t.mock.method(globalThis, "fetch", …)fromnode:test, which restores itself when the test ends. Do not hand-roll aglobalThis.fetchswap. fetchModelsreads a key only from thecredentialargument. Env resolution of$NOUS_API_KEYisenvApiKeyAuth's job upstream. The one test that touchesprocess.envsets it deliberately, to pin thatfetchModelsignores it; it restores the previous value int.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.tsstarts importing it at module load.
Verification
npm test(ornpm run check) runsnode --test tests/*.test.mjs. All tests must pass.
Child DOX Index
| Path | Scope |
|---|---|
| (none) | stubs/ holds a single file governed by the contracts above |