Skip to content

Commit 615da03

Browse files
author
Martin
committed
Fix tests for Vitest and adjust guard expectations
1 parent 721153e commit 615da03

File tree

5 files changed

+30
-29
lines changed

5 files changed

+30
-29
lines changed

bun.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
"build": "bunup",
4949
"watch": "tsc --watch",
5050
"inspector": "npx @modelcontextprotocol/inspector dist/index.js",
51-
"test": "bun test",
52-
"test:watch": "bun test --watch",
53-
"test:cov": "bun test --coverage",
51+
"test": "bunx vitest run",
52+
"test:watch": "bunx vitest watch",
53+
"test:cov": "bunx vitest run --coverage",
5454
"lint": "biome lint .",
5555
"lint:fix": "biome lint --write .",
5656
"format": "biome format --write .",

test/handlers/pageGuards.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ vi.mock('../../src/pdf/loader.js', () => ({
2929
}));
3030

3131
vi.mock('../../src/pdf/extractor.js', async () => {
32-
const actual = await vi.importActual<typeof import('../../src/pdf/extractor.js')>('../../src/pdf/extractor.js');
3332
return {
34-
...actual,
33+
buildWarnings: (invalidPages: number[], totalPages: number) => {
34+
if (invalidPages.length === 0) return [];
35+
return [`Requested page numbers ${invalidPages.join(', ')} exceed total pages (${String(totalPages)}).`];
36+
},
3537
extractPageContent: mockExtractPageContent,
3638
extractImages: mockExtractImages,
3739
extractMetadataAndPageCount: mockExtractMetadataAndPageCount,

test/handlers/readPdf.test.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ const mockGetMetadata = vi.fn();
1414
const mockGetPage = vi.fn();
1515
const mockGetDocument = vi.fn();
1616
const mockReadFile = vi.fn();
17+
const mockStat = vi.fn();
1718
const mockExtractPageContent = vi.fn();
1819
let actualExtractPageContent: typeof import('../../src/pdf/extractor.js')['extractPageContent'];
20+
let extractorModule: typeof import('../../src/pdf/extractor.js');
1921

2022
vi.mock('pdfjs-dist/legacy/build/pdf.mjs', () => ({
2123
getDocument: mockGetDocument,
@@ -28,19 +30,12 @@ vi.mock('pdfjs-dist/legacy/build/pdf.mjs', () => ({
2830
vi.mock('node:fs/promises', () => ({
2931
default: {
3032
readFile: mockReadFile,
33+
stat: mockStat,
3134
},
3235
readFile: mockReadFile,
36+
stat: mockStat,
3337
}));
3438

35-
vi.mock('../../src/pdf/extractor.js', async () => {
36-
const actual = await vi.importActual<typeof import('../../src/pdf/extractor.js')>('../../src/pdf/extractor.js');
37-
actualExtractPageContent = actual.extractPageContent;
38-
return {
39-
...actual,
40-
extractPageContent: mockExtractPageContent,
41-
};
42-
});
43-
4439
// Dynamically import the handler *once* after mocks are defined
4540
// Define a more specific type for the handler's return value content
4641
interface HandlerResultContent {
@@ -51,6 +46,10 @@ let handler: (args: unknown) => Promise<{ content: HandlerResultContent[] }>;
5146
let readPdfSchema: Schema<unknown>;
5247

5348
beforeAll(async () => {
49+
extractorModule = await import('../../src/pdf/extractor.js');
50+
actualExtractPageContent = extractorModule.extractPageContent;
51+
vi.spyOn(extractorModule, 'extractPageContent').mockImplementation(mockExtractPageContent);
52+
5453
// Import the readPdf tool - the new SDK uses a builder pattern
5554
const { readPdf } = await import('../../src/handlers/readPdf.js');
5655
const { readPdfArgsSchema } = await import('../../src/schemas/readPdf.js');
@@ -90,6 +89,7 @@ describe('handleReadPdfFunc Integration Tests', () => {
9089
beforeEach(() => {
9190
vi.resetAllMocks();
9291
mockExtractPageContent.mockImplementation((...args) => actualExtractPageContent(...args));
92+
vi.spyOn(extractorModule, 'extractPageContent').mockImplementation(mockExtractPageContent);
9393
// Reset mocks for pathUtils if we spy on it
9494
vi.spyOn(pathUtils, 'resolvePath').mockImplementation((p) => p); // Simple mock for resolvePath
9595

@@ -164,6 +164,9 @@ describe('handleReadPdfFunc Integration Tests', () => {
164164
metadata: { 'dc:format': 'application/pdf' },
165165
num_pages: 3,
166166
full_text: 'Mock page text 1\n\nMock page text 2\n\nMock page text 3',
167+
warnings: [
168+
'No pages specified; processed available pages because the document is small. Specify pages or set allow_full_document=true to control full-document requests.',
169+
],
167170
},
168171
},
169172
],
@@ -316,6 +319,7 @@ describe('handleReadPdfFunc Integration Tests', () => {
316319
const args = {
317320
sources: [{ path: 'many-pages.pdf' }],
318321
include_full_text: true,
322+
allow_full_document: true,
319323
};
320324

321325
const result = await handler(args);
@@ -467,6 +471,9 @@ describe('handleReadPdfFunc Integration Tests', () => {
467471
metadata: { 'dc:creator': 'URL Author' },
468472
num_pages: 2,
469473
full_text: 'URL Mock page text 1\n\nURL Mock page text 2',
474+
warnings: [
475+
'No pages specified; processed available pages because the document is small. Specify pages or set allow_full_document=true to control full-document requests.',
476+
],
470477
},
471478
},
472479
],

test/pdf/extractor.test.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ describe('extractor', () => {
193193
});
194194

195195
it('processes pages in batches to avoid unbounded work on large documents', async () => {
196-
vi.useFakeTimers();
197-
198196
let activeRequests = 0;
199197
let maxConcurrentRequests = 0;
200198

@@ -209,28 +207,21 @@ describe('extractor', () => {
209207
activeRequests++;
210208
maxConcurrentRequests = Math.max(maxConcurrentRequests, activeRequests);
211209

212-
return new Promise((resolve) =>
210+
return new Promise((resolve) => {
213211
setTimeout(() => {
214212
activeRequests--;
215213
resolve(mockPage);
216-
}, 10)
217-
);
214+
}, 5);
215+
});
218216
}),
219217
} as unknown as pdfjsLib.PDFDocumentProxy;
220218

221219
const pages = Array.from({ length: 10 }, (_, idx) => idx + 1);
222220

223-
try {
224-
const extractionPromise = extractPageTexts(mockDocument, pages, 'large.pdf');
225-
226-
await vi.runAllTimersAsync();
227-
const result = await extractionPromise;
221+
const result = await extractPageTexts(mockDocument, pages, 'large.pdf');
228222

229-
expect(maxConcurrentRequests).toBeLessThanOrEqual(6);
230-
expect(result).toHaveLength(10);
231-
} finally {
232-
vi.useRealTimers();
233-
}
223+
expect(maxConcurrentRequests).toBeLessThanOrEqual(6);
224+
expect(result).toHaveLength(10);
234225
});
235226
});
236227

0 commit comments

Comments
 (0)