Give login an optional clock ({ now, sleep }) with the real timers as
the default, so the device-code poll loop can be tested without
wall-clock time. Neither t.mock.timers nor jiti aliasing reaches
node:timers/promises, so a seam is the only way in; auth.oauth.login
still passes only the interaction, so pi's interface is unchanged.
Six tests cover pending to slow_down to success, the interval fallback
and floor, the deadline cap, a non-retryable error and a non-OK device
code response. The token mock gives up after 100 polls: an
auto-advancing fake starves the event loop, so a sleep-less regression
would otherwise hang the suite with no test timeout able to fire.
3.6 KiB
3.6 KiB
tests/AGENTS.md
Purpose
Offline unit tests for index.ts. They pin the pure logic that has real branching: model mapping, credential construction, the device-code poll loop, model fetching, token-endpoint error handling, and the slash commands (driven through a recording pi). No network, no real credentials, no wall-clock time.
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,login,tokenRequest,fetchModels,tierOf, and the default export via thedrive()harness.drive(flag)returnsrun(name, model, available, refresh)— pass a deferred promise asrefreshto simulate overlapping command invocations, and a Nousmodelwhen thesetModelhalf of a contract is under test — plus the registeredprovider, which is how the provider-levelfetchModelslast-good retention is reached. logintakes an optional clock ({ now, sleep }) and defaults to real timers. Five of the sixlogintests drive it withdriveLogin(t, replies, device); the sixth exercises the default clock: an auto-advancing clock (each sleep adds its ms tonow) plus a mocked device-code and token endpoint, where the last reply repeats and the mock gives up after 100 token polls, so a sleep-less regression fails instead of hanging (the fake clock starves the event loop; no test timeout can fire). Never wait on wall-clock time in tests:t.mock.timersdoes not capturenode:timers/promisesand jiti does not aliasnode:builtins (both probed on Node 26).- 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 |