read-pdf.test.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. getReadInputValidationFailure,
  9. getReadPdfPagesValidationFailure,
  10. parseReadPdfPageRange,
  11. } from "../src/tools/read.js";
  12. describe("Read PDF contract", () => {
  13. it("keeps pages out of the legacy Read schema and exposes the exact PDF schema field", () => {
  14. expect(ReadInputJsonSchema.properties).not.toHaveProperty("pages");
  15. expect(ReadPdfInputJsonSchema.properties).toMatchObject({
  16. pages: {
  17. description: READ_PDF_PAGES_DESCRIPTION,
  18. type: "string",
  19. },
  20. });
  21. expect(READ_PDF_PAGES_DESCRIPTION).toBe(
  22. 'Page range for PDF files (e.g., "1-5", "3", "10-20"). Only applicable to PDF files. Maximum 20 pages per request.',
  23. );
  24. });
  25. it("parses PDF page syntax and rejects invalid or over-wide ranges", () => {
  26. expect(parseReadPdfPageRange("3")).toEqual({ firstPage: 3, lastPage: 3 });
  27. expect(parseReadPdfPageRange("2-5")).toEqual({ firstPage: 2, lastPage: 5 });
  28. expect(parseReadPdfPageRange("10-")).toEqual({ firstPage: 10, lastPage: Infinity });
  29. expect(parseReadPdfPageRange("0")).toBeUndefined();
  30. expect(parseReadPdfPageRange("5-4")).toBeUndefined();
  31. // 页码检查在 Read validateInput(getReadInputValidationFailure),runtime schema 不再拒绝。
  32. expect(getReadInputValidationFailure("/tmp/report.pdf", "1-20")).toBeUndefined();
  33. expect(
  34. getReadInputValidationFailure("/tmp/report.pdf", `1-${READ_PDF_MAX_PAGES_PER_REQUEST + 1}`),
  35. ).toEqual({
  36. errorCode: 8,
  37. message:
  38. 'Page range "1-21" exceeds maximum of 20 pages per request. Please use a smaller range.',
  39. });
  40. expect(getReadInputValidationFailure("/tmp/report.pdf", "10-")?.message).toBe(
  41. 'Page range "10-" exceeds maximum of 20 pages per request. Please use a smaller range.',
  42. );
  43. expect(ReadInputSchema.safeParse({ file_path: "/tmp/report.pdf", pages: "1-21" }).success).toBe(
  44. true,
  45. );
  46. });
  47. it("does not apply PDF page validation to non-PDF compatibility calls", () => {
  48. expect(getReadInputValidationFailure("/tmp/notes.txt", "not-a-range")).toBeUndefined();
  49. });
  50. it("returns the binary and device path failures with their error codes", () => {
  51. expect(getReadInputValidationFailure("/tmp/archive.zip")).toEqual({
  52. errorCode: 4,
  53. message:
  54. "This tool cannot read binary files. The file appears to be a binary .zip file. Use a skill for this file type if one is available, or a shell command or script that can read the format.",
  55. });
  56. expect(getReadInputValidationFailure("/dev/zero")).toEqual({
  57. errorCode: 9,
  58. message:
  59. "Cannot read '/dev/zero': this path names a device or process file that this tool does not read.",
  60. });
  61. expect(getReadInputValidationFailure("/tmp/notes.txt")).toBeUndefined();
  62. expect(ReadInputSchema.safeParse({ file_path: "/dev/zero" }).success).toBe(true);
  63. });
  64. it("returns the exact validation messages and error codes for PDF pages", () => {
  65. expect(getReadPdfPagesValidationFailure("/tmp/report.pdf", "invalid")).toEqual({
  66. errorCode: 7,
  67. message:
  68. 'Invalid pages parameter: "invalid". Give one page ("3") or one range ("1-5"), not a list. To read several pages or ranges, read each one separately. Pages are 1-indexed.',
  69. });
  70. expect(getReadPdfPagesValidationFailure("/tmp/report.pdf", "1-21")).toEqual({
  71. errorCode: 8,
  72. message:
  73. 'Page range "1-21" exceeds maximum of 20 pages per request. Please use a smaller range.',
  74. });
  75. expect(getReadPdfPagesValidationFailure("/tmp/notes.txt", "invalid")).toBeUndefined();
  76. });
  77. });