Skip to content

Commit c32534f

Browse files
chore: fix existing tests
1 parent 042aa1d commit c32534f

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

src/tools/mongodb/read/aggregate.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,21 @@ export class AggregateTool extends MongoDBToolBase {
6060
iterateCursorUntilMaxBytes(aggregationCursor, this.config.maxBytesPerQuery),
6161
]);
6262

63-
const messageDescription = `\
64-
The aggregation resulted in ${totalDocuments === undefined ? "indeterminable number of" : totalDocuments} documents. \
65-
Returning ${documents.length} documents while respecting the applied limits. \
66-
Note to LLM: If entire aggregation result is needed then use "export" tool to export the aggregation results.\
67-
`;
63+
let messageDescription = `\
64+
The aggregation resulted in ${totalDocuments === undefined ? "indeterminable number of" : totalDocuments} documents.\
65+
`;
66+
if (documents.length) {
67+
messageDescription += ` \
68+
Returning ${documents.length} documents while respecting the applied limits. \
69+
Note to LLM: If entire aggregation result is needed then use "export" tool to export the aggregation results.\
70+
`;
71+
}
6872

6973
return {
70-
content: formatUntrustedData(messageDescription, EJSON.stringify(documents)),
74+
content: formatUntrustedData(
75+
messageDescription,
76+
documents.length > 0 ? EJSON.stringify(documents) : undefined
77+
),
7178
};
7279
} finally {
7380
await aggregationCursor?.close();
@@ -114,7 +121,7 @@ export class AggregateTool extends MongoDBToolBase {
114121
"totalDocuments" in documentWithCount &&
115122
typeof documentWithCount.totalDocuments === "number"
116123
? documentWithCount.totalDocuments
117-
: undefined;
124+
: 0;
118125

119126
return totalDocuments;
120127
}, undefined);

src/tools/mongodb/read/find.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,21 @@ export class FindTool extends MongoDBToolBase {
8989
iterateCursorUntilMaxBytes(findCursor, this.config.maxBytesPerQuery),
9090
]);
9191

92-
const messageDescription = `\
93-
Query on collection "${collection}" resulted in ${queryResultsCount === undefined ? "indeterminable number of" : queryResultsCount} documents. \
92+
let messageDescription = `\
93+
Query on collection "${collection}" resulted in ${queryResultsCount === undefined ? "indeterminable number of" : queryResultsCount} documents.\
94+
`;
95+
if (documents.length) {
96+
messageDescription += ` \
9497
Returning ${documents.length} documents while respecting the applied limits. \
9598
Note to LLM: If entire query result is needed then use "export" tool to export the query results.\
9699
`;
100+
}
97101

98102
return {
99-
content: formatUntrustedData(messageDescription, EJSON.stringify(documents)),
103+
content: formatUntrustedData(
104+
messageDescription,
105+
documents.length > 0 ? EJSON.stringify(documents) : undefined
106+
),
100107
};
101108
} finally {
102109
await findCursor?.close();

tests/integration/indexCheck.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ describe("IndexCheck integration tests", () => {
6161

6262
expect(response.isError).toBeFalsy();
6363
const content = getResponseContent(response.content);
64-
expect(content).toContain("Found");
65-
expect(content).toContain("documents");
64+
expect(content).toContain('Query on collection "find-test-collection" resulted in');
6665
});
6766

6867
it("should allow queries using _id (IDHACK)", async () => {
@@ -86,7 +85,9 @@ describe("IndexCheck integration tests", () => {
8685

8786
expect(response.isError).toBeFalsy();
8887
const content = getResponseContent(response.content);
89-
expect(content).toContain("Found 1 documents");
88+
expect(content).toContain(
89+
'Query on collection "find-test-collection" resulted in 1 documents.'
90+
);
9091
});
9192
});
9293

@@ -351,7 +352,7 @@ describe("IndexCheck integration tests", () => {
351352

352353
expect(findResponse.isError).toBeFalsy();
353354
const findContent = getResponseContent(findResponse.content);
354-
expect(findContent).toContain("Found");
355+
expect(findContent).toContain('Query on collection "disabled-test-collection" resulted in');
355356
expect(findContent).not.toContain("Index check failed");
356357
});
357358

tests/integration/tools/mongodb/read/find.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describeWithMongoDB("find tool", (integration) => {
5656
arguments: { database: "non-existent", collection: "foos" },
5757
});
5858
const content = getResponseContent(response.content);
59-
expect(content).toEqual('Found 0 documents in the collection "foos".');
59+
expect(content).toEqual('Query on collection "foos" resulted in 0 documents.');
6060
});
6161

6262
it("returns 0 when collection doesn't exist", async () => {
@@ -68,7 +68,7 @@ describeWithMongoDB("find tool", (integration) => {
6868
arguments: { database: integration.randomDbName(), collection: "non-existent" },
6969
});
7070
const content = getResponseContent(response.content);
71-
expect(content).toEqual('Found 0 documents in the collection "non-existent".');
71+
expect(content).toEqual('Query on collection "non-existent" resulted in 0 documents.');
7272
});
7373

7474
describe("with existing database", () => {
@@ -148,7 +148,7 @@ describeWithMongoDB("find tool", (integration) => {
148148
},
149149
});
150150
const content = getResponseContent(response);
151-
expect(content).toContain(`Found ${expected.length} documents in the collection "foo".`);
151+
expect(content).toContain(`Query on collection "foo" resulted in ${expected.length} documents.`);
152152

153153
const docs = getDocsFromUntrustedContent(content);
154154

@@ -165,7 +165,7 @@ describeWithMongoDB("find tool", (integration) => {
165165
arguments: { database: integration.randomDbName(), collection: "foo" },
166166
});
167167
const content = getResponseContent(response);
168-
expect(content).toContain('Found 10 documents in the collection "foo".');
168+
expect(content).toContain('Query on collection "foo" resulted in 10 documents.');
169169

170170
const docs = getDocsFromUntrustedContent(content);
171171
expect(docs.length).toEqual(10);
@@ -195,7 +195,7 @@ describeWithMongoDB("find tool", (integration) => {
195195
});
196196

197197
const content = getResponseContent(response);
198-
expect(content).toContain('Found 1 documents in the collection "foo".');
198+
expect(content).toContain('Query on collection "foo" resulted in 1 documents.');
199199

200200
const docs = getDocsFromUntrustedContent(content);
201201
expect(docs.length).toEqual(1);
@@ -207,7 +207,7 @@ describeWithMongoDB("find tool", (integration) => {
207207
validateAutoConnectBehavior(integration, "find", () => {
208208
return {
209209
args: { database: integration.randomDbName(), collection: "coll1" },
210-
expectedResponse: 'Found 0 documents in the collection "coll1"',
210+
expectedResponse: 'Query on collection "coll1" resulted in 0 documents.',
211211
};
212212
});
213213
});

0 commit comments

Comments
 (0)