Implement src/defaults.ts with unit tests (FIND_LIMIT, OUTPUT_BYTE_CAP) #137

Closed
opened 2026-08-31 22:03:58 +00:00 by david · 1 comment
Owner

Summary

Implement extensions/mongodb/src/defaults.ts — the shared constants (FIND_LIMIT, OUTPUT_BYTE_CAP) and the applyDefaultAndCap helper used by mongo_find's limit handling — with unit tests.

Background

Depends on: #133

These numbers bound how much data the tools return, protecting the LLM's context window from unbounded results. They are deliberately conservative working defaults (tunable constants), not the result of load testing. mongo_find (a later step) applies FIND_LIMIT to its limit parameter; src/serialize.ts (the sibling step in this milestone) uses OUTPUT_BYTE_CAP for its byte cap. No external packages are used by this module — pure logic.

Implementation Details

export const FIND_LIMIT = { default: 100, max: 1000 } as const;
export const OUTPUT_BYTE_CAP = 100_000 as const; // 100 KB

export function applyDefaultAndCap(
  value: number | undefined,
  { default: def, max }: { default: number; max: number }
): number

Rules, applied uniformly:

  1. Omitted/undefineddefault (100).
  2. Above max (1000) → clamped to maxnot rejected (keeps the call ergonomic while bounding result size).
  3. Within range → unchanged.
  4. 0 is an explicit value — must return 0, NOT fall through to the default. Guard against ??-vs-falsy bugs: use value === undefined checks, never truthiness.

Write co-located tests in src/defaults.test.ts (bun test) covering each rule, including the 0 case.

Acceptance Criteria

  • Omitted/undefined limit → returns the default (100).
  • limit above max → clamped to max (1000).
  • limit within range → unchanged.
  • limit: 0 → returns 0 (explicit value, not the default).
  • bun test in extensions/mongodb/ is green for src/defaults.ts.

Test Plan

cd extensions/mongodb
bun test   # defaults tests green
## Summary Implement `extensions/mongodb/src/defaults.ts` — the shared constants (`FIND_LIMIT`, `OUTPUT_BYTE_CAP`) and the `applyDefaultAndCap` helper used by `mongo_find`'s limit handling — with unit tests. ## Background **Depends on:** #133 These numbers bound how much data the tools return, protecting the LLM's context window from unbounded results. They are deliberately conservative working defaults (tunable constants), not the result of load testing. `mongo_find` (a later step) applies `FIND_LIMIT` to its `limit` parameter; `src/serialize.ts` (the sibling step in this milestone) uses `OUTPUT_BYTE_CAP` for its byte cap. No external packages are used by this module — pure logic. ## Implementation Details ```ts export const FIND_LIMIT = { default: 100, max: 1000 } as const; export const OUTPUT_BYTE_CAP = 100_000 as const; // 100 KB export function applyDefaultAndCap( value: number | undefined, { default: def, max }: { default: number; max: number } ): number ``` Rules, applied uniformly: 1. Omitted/`undefined` → `default` (100). 2. Above `max` (1000) → clamped to `max` — **not** rejected (keeps the call ergonomic while bounding result size). 3. Within range → unchanged. 4. `0` is an explicit value — must return `0`, NOT fall through to the default. Guard against `??`-vs-falsy bugs: use `value === undefined` checks, never truthiness. Write co-located tests in `src/defaults.test.ts` (bun test) covering each rule, including the `0` case. ## Acceptance Criteria - [ ] Omitted/`undefined` limit → returns the default (100). - [ ] `limit` above `max` → clamped to `max` (1000). - [ ] `limit` within range → unchanged. - [ ] `limit: 0` → returns `0` (explicit value, not the default). - [ ] `bun test` in `extensions/mongodb/` is green for `src/defaults.ts`. ## Test Plan ```bash cd extensions/mongodb bun test # defaults tests green ```
Author
Owner

Closing — all acceptance criteria verified:

  • extensions/mongodb/src/defaults.ts committed (commit 9bd85ee) with FIND_LIMIT = { default: 100, max: 1000 }, OUTPUT_BYTE_CAP = 100_000, and applyDefaultAndCap().
  • All four rules implemented: undefined → default (via value === undefined check, not truthiness), above max → clamped to max, within range → unchanged, explicit 0 → returns 0. Bonus: negative/NaN fall back to the default.
  • Co-located tests in src/defaults.test.ts cover every rule including the 0 case — bun test src/defaults.test.ts: 9 pass, 0 fail.
  • Dependency #133 (package init) is closed.
Closing — all acceptance criteria verified: - `extensions/mongodb/src/defaults.ts` committed (commit `9bd85ee`) with `FIND_LIMIT = { default: 100, max: 1000 }`, `OUTPUT_BYTE_CAP = 100_000`, and `applyDefaultAndCap()`. - All four rules implemented: `undefined` → default (via `value === undefined` check, not truthiness), above max → clamped to max, within range → unchanged, explicit `0` → returns `0`. Bonus: negative/NaN fall back to the default. - Co-located tests in `src/defaults.test.ts` cover every rule including the `0` case — `bun test src/defaults.test.ts`: **9 pass, 0 fail**. - Dependency #133 (package init) is closed.
david closed this issue 2026-09-18 06:49:26 +00:00
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
david/pi-extensions-and-skills#137
No description provided.