toolImageFailure.test.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { act, cleanup, render, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import type { FileMediaPreview } from "@zcode/shared";
  6. import { ToolCallBody } from "@/ToolCallBlocks/ToolCallBody.js";
  7. import {
  8. ToolPresentationCapabilitiesProvider,
  9. type ToolPresentationCapabilities,
  10. } from "@/ToolCallBlocks/presentationCapabilities.js";
  11. vi.mock("@/i18n/IntlProvider.js", () => ({
  12. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  13. }));
  14. afterEach(cleanup);
  15. function body(capabilities: ToolPresentationCapabilities, path = "/workspace/a.png") {
  16. return createElement(
  17. ToolPresentationCapabilitiesProvider,
  18. { value: capabilities },
  19. createElement(ToolCallBody, {
  20. childToolList: null,
  21. workspacePath: "/workspace",
  22. toolCall: { toolId: "image", kind: "read", status: "completed" },
  23. displayModel: {
  24. inlinePreview: { type: "image", source: { type: "image", path, title: path } },
  25. planResult: null,
  26. viewerSource: null,
  27. viewerLabelId: "codeViewer.viewCode",
  28. showSummaryFileLink: false,
  29. showInput: false,
  30. showOutput: false,
  31. showKind: false,
  32. },
  33. }),
  34. );
  35. }
  36. const media = (path: string): FileMediaPreview => ({
  37. path,
  38. mediaType: "image/png",
  39. dataBase64: `data:image/png;base64,${path}`,
  40. totalBytes: 1,
  41. });
  42. describe("inline tool image lifecycle", () => {
  43. it("renders unavailable without a reader", () => {
  44. render(body({}));
  45. expect(screen.getByText("chat.toolCall.previewUnavailable")).toBeTruthy();
  46. });
  47. it("settles empty results as unavailable", async () => {
  48. render(body({ readMediaPreview: async () => ({ ...media("a"), dataBase64: "" }) }));
  49. expect(await screen.findByText("chat.toolCall.previewUnavailable")).toBeTruthy();
  50. });
  51. it("handles rejected reads and exits loading", async () => {
  52. render(
  53. body({
  54. readMediaPreview: async () => {
  55. throw new Error("read failed");
  56. },
  57. }),
  58. );
  59. expect(await screen.findByText("chat.toolCall.previewFailed")).toBeTruthy();
  60. });
  61. it("uses a public URL without reading workspace files", () => {
  62. const readMediaPreview = vi.fn(async () => media("unused"));
  63. render(body({ readMediaPreview, resourceUrl: () => "https://example.com/image.png" }));
  64. expect(screen.getByRole("img").getAttribute("src")).toBe("https://example.com/image.png");
  65. expect(readMediaPreview).not.toHaveBeenCalled();
  66. });
  67. it("discards stale reads and clears the image when the path or reader changes", async () => {
  68. let resolveOld!: (value: FileMediaPreview) => void;
  69. const readMediaPreview = vi.fn((path: string) =>
  70. path.endsWith("a.png")
  71. ? new Promise<FileMediaPreview>((resolve) => {
  72. resolveOld = resolve;
  73. })
  74. : Promise.resolve(media("new")),
  75. );
  76. const view = render(body({ readMediaPreview }));
  77. view.rerender(body({ readMediaPreview }, "/workspace/b.png"));
  78. await screen.findByRole("img");
  79. await act(async () => resolveOld(media("old")));
  80. expect(screen.getByRole("img").getAttribute("src")).toBe(media("new").dataBase64);
  81. view.rerender(body({}, "/workspace/b.png"));
  82. expect(screen.queryByRole("img")).toBeNull();
  83. expect(screen.getByText("chat.toolCall.previewUnavailable")).toBeTruthy();
  84. });
  85. });