Implement the mongo_list_collections tool with unit tests #142

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

Summary

Implement the mongo_list_collections tool — list the collections in a database, optionally name-only — with unit tests against a fake client.

Background

Depends on: #139

Part of the read-only tool surface of the mongodb extension. Answers "what collections exist in this database". The read-only boundary is enforced by construction: the tool calls only db.listCollections() — no schema/write operations are reachable. db is required; nameOnly lets the caller request just names for a smaller payload. The extension connects lazily, so connection/auth failures surface here as categorized ToolErrors thrown from execute() (see src/errors.ts).

Documentation Required

A separate process downloads these into the listed folder before this issue is implemented. Check the folder for the actual reference material before starting.

docs/reference/mongodb-driver/ — for Db.listCollections:

Implementation Details

Create extensions/mongodb/src/tools/listCollections.ts. Tool registered as mongo_list_collections via pi.registerTool with a TypeBox parameter schema:

  • db (string, required) — database name
  • nameOnly? (boolean, default false)

Behavior:

  1. const infos = await db.listCollections({}, { nameOnly }).toArray() — wrap in try/catch; on error toToolError(err, context) (throws).
  2. When nameOnly: true, the driver returns { name, type }[] — return those (the names) directly; otherwise return the full info documents { name, type, options, info }[].
  3. Return the EJSON array as content and details: { count } (number of collections).

Mirror the structure of src/tools/find.ts / src/tools/count.ts (sibling steps) and the sibling extensions/postgres/src/tools/ implementations. Tests use a fake client whose db(name) returns an object with listCollections(filter, options) returning a cursor with toArray().

Write co-located tests in src/tools/listCollections.test.ts:

  • Happy path: fake returns collection info docs → EJSON array content + details: { count } correct.
  • nameOnly: true → names only returned.
  • Each error category (connection, auth, invalid, server, unexpected) surfaces as the categorized message.

Acceptance Criteria

  • Happy path returns the collection info documents as EJSON and details: { count }.
  • nameOnly: true → only collection names returned.
  • All five error categories throw categorized ToolErrors with correct message prefixes.
  • bun test in extensions/mongodb/ is green for src/tools/listCollections.ts.

Test Plan

cd extensions/mongodb
bun test   # listCollections tool tests green
## Summary Implement the `mongo_list_collections` tool — list the collections in a database, optionally name-only — with unit tests against a fake client. ## Background **Depends on:** #139 Part of the read-only tool surface of the mongodb extension. Answers "what collections exist in this database". The read-only boundary is enforced by construction: the tool calls only `db.listCollections()` — no schema/write operations are reachable. `db` is required; `nameOnly` lets the caller request just names for a smaller payload. The extension connects lazily, so connection/auth failures surface here as categorized `ToolError`s thrown from `execute()` (see `src/errors.ts`). ## Documentation Required A separate process downloads these into the listed folder before this issue is implemented. Check the folder for the actual reference material before starting. **`docs/reference/mongodb-driver/`** — for `Db.listCollections`: - https://mongodb.github.io/node-mongodb-native/7.6/classes/Db.html — API reference for `Db.listCollections(filter?, options?)` → `ListCollectionsCursor`; `ListCollectionsOptions` includes `nameOnly` (when true, returns `{ name, type }[]` instead of full info documents `{ name, type, options, info }`). - https://www.mongodb.com/docs/drivers/node/current/databases-collections/ — guide: working with databases and collections via the driver. - https://mongodb.github.io/node-mongodb-native/7.6/ — TypeDoc API reference index (cursor class). ## Implementation Details Create `extensions/mongodb/src/tools/listCollections.ts`. Tool registered as `mongo_list_collections` via `pi.registerTool` with a TypeBox parameter schema: - `db` (string, **required**) — database name - `nameOnly?` (boolean, default `false`) Behavior: 1. `const infos = await db.listCollections({}, { nameOnly }).toArray()` — wrap in try/catch; on error `toToolError(err, context)` (throws). 2. When `nameOnly: true`, the driver returns `{ name, type }[]` — return those (the names) directly; otherwise return the full info documents `{ name, type, options, info }[]`. 3. Return the EJSON array as content and `details: { count }` (number of collections). Mirror the structure of `src/tools/find.ts` / `src/tools/count.ts` (sibling steps) and the sibling `extensions/postgres/src/tools/` implementations. Tests use a fake client whose `db(name)` returns an object with `listCollections(filter, options)` returning a cursor with `toArray()`. Write co-located tests in `src/tools/listCollections.test.ts`: - Happy path: fake returns collection info docs → EJSON array content + `details: { count }` correct. - `nameOnly: true` → names only returned. - Each error category (connection, auth, invalid, server, unexpected) surfaces as the categorized message. ## Acceptance Criteria - [ ] Happy path returns the collection info documents as EJSON and `details: { count }`. - [ ] `nameOnly: true` → only collection names returned. - [ ] All five error categories throw categorized `ToolError`s with correct message prefixes. - [ ] `bun test` in `extensions/mongodb/` is green for `src/tools/listCollections.ts`. ## Test Plan ```bash cd extensions/mongodb bun test # listCollections tool tests green ```
david closed this issue 2026-09-01 00:29:49 +00:00
Author
Owner

pi-loop opened and merged a pull request for this issue: #159

pi-loop opened and merged a pull request for this issue: https://git.excelera.net/david/pi-extensions-and-skills/pulls/159
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#142
No description provided.