read-pdf.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. READ_PDF_MAX_PAGES_PER_REQUEST,
  4. READ_PDF_PAGES_DESCRIPTION,
  5. ReadInputJsonSchema,
  6. ReadInputSchema,
  7. ReadPdfInputJsonSchema,
  8. getReadPdfPagesValidationFailure,
  9. parseReadPdfPageRange,
  10. } from "../src/tools/read.js";
  11. describe("Read PDF contract", () => {
  12. it("keeps pages out of the legacy Read schema and exposes the exact PDF schema field", () => {
  13. expect(ReadInputJsonSchema.properties).not.toHaveProperty("pages");
  14. expect(ReadPdfInputJsonSchema.properties).toMatchObject({
  15. pages: {
  16. description: READ_PDF_PAGES_DESCRIPTION,
  17. type: "string",
  18. },
  19. });
  20. expect(READ_PDF_PAGES_DESCRIPTION).toBe(
  21. 'Page range for PDF files (e.g., "1-5", "3", "10-20"). Only applicable to PDF files. Maximum 20 pages per request.',
  22. );
  23. });
  24. it("parses PDF page syntax and rejects invalid or over-wide ranges", () => {
  25. expect(parseReadPdfPageRange("3")).toEqual({ firstPage: 3, lastPage: 3 });
  26. expect(parseReadPdfPageRange("2-5")).toEqual({ firstPage: 2, lastPage: 5 });
  27. expect(parseReadPdfPageRange("10-")).toEqual({ firstPage: 10, lastPage: Infinity });
  28. expect(parseReadPdfPageRange("0")).toBeUndefined();
  29. expect(parseReadPdfPageRange("5-4")).toBeUndefined();
  30. expect(ReadInputSchema.safeParse({ file_path: "/tmp/report.pdf", pages: "1-20" }).success).toBe(
  31. true,
  32. );
  33. const tooWide = ReadInputSchema.safeParse({
  34. file_path: "/tmp/report.pdf",
  35. pages: `1-${READ_PDF_MAX_PAGES_PER_REQUEST + 1}`,
  36. });
  37. expect(tooWide.success).toBe(false);
  38. expect(tooWide.error?.issues[0]?.message).toBe(
  39. 'Page range "1-21" exceeds maximum of 20 pages per request. Please use a smaller range.',
  40. );
  41. expect(
  42. ReadInputSchema.safeParse({ file_path: "/tmp/report.pdf", pages: "10-" }).error?.issues[0]
  43. ?.message,
  44. ).toBe('Page range "10-" exceeds maximum of 20 pages per request. Please use a smaller range.');
  45. });
  46. it("does not apply PDF page validation to non-PDF compatibility calls", () => {
  47. expect(
  48. ReadInputSchema.safeParse({ file_path: "/tmp/notes.txt", pages: "not-a-range" }).success,
  49. ).toBe(true);
  50. });
  51. it("returns the exact validation messages and error codes for PDF pages", () => {
  52. expect(getReadPdfPagesValidationFailure("/tmp/report.pdf", "invalid")).toEqual({
  53. errorCode: 7,
  54. message:
  55. 'Invalid pages parameter: "invalid". Use formats like "1-5", "3", or "10-20". Pages are 1-indexed.',
  56. });
  57. expect(getReadPdfPagesValidationFailure("/tmp/report.pdf", "1-21")).toEqual({
  58. errorCode: 8,
  59. message:
  60. 'Page range "1-21" exceeds maximum of 20 pages per request. Please use a smaller range.',
  61. });
  62. expect(getReadPdfPagesValidationFailure("/tmp/notes.txt", "invalid")).toBeUndefined();
  63. });
  64. });