mediaPreviewService.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createMediaPreviewService } from "../src/media-preview/mediaPreview.js";
  3. import type { IFileService } from "../src/file/file.js";
  4. function createFileService(overrides: Partial<IFileService> = {}): IFileService {
  5. return {
  6. readdir: vi.fn(),
  7. stat: vi.fn(async () => ({ path: "/workspace/song.mp3", type: "file", size: 3 })),
  8. checkFilesExist: vi.fn(),
  9. resolvePath: vi.fn(),
  10. ensureConversationWorkspace: vi.fn(),
  11. createDefaultWorkspace: vi.fn(),
  12. createScratchWorkspace: vi.fn(),
  13. readTextFile: vi.fn(),
  14. readMediaPreview: vi.fn(async ({ path }) => ({
  15. path,
  16. mediaType: "audio/mpeg",
  17. dataBase64: "AQID",
  18. totalBytes: 3,
  19. })),
  20. readFileRange: vi.fn(),
  21. readBinaryPreview: vi.fn(),
  22. listWorkspaceFiles: vi.fn(),
  23. ...overrides,
  24. } as unknown as IFileService;
  25. }
  26. describe("media preview service", () => {
  27. it("authorizes local files before returning a playable URL", async () => {
  28. const fileService = createFileService();
  29. const authorize = vi.fn(async () => "/canonical/song.mp3");
  30. const service = createMediaPreviewService({
  31. fileService,
  32. authorizeLocalMediaPreviewPath: authorize,
  33. createLocalMediaPreviewUrl: (path) => `zcode-media://local/preview?path=${path}`,
  34. });
  35. await expect(
  36. service.prepare({ path: "/workspace/song.mp3", expectedKind: "audio" }),
  37. ).resolves.toEqual({
  38. kind: "local-url",
  39. mediaType: "audio/mpeg",
  40. path: "/canonical/song.mp3",
  41. size: 3,
  42. url: "zcode-media://local/preview?path=/canonical/song.mp3",
  43. });
  44. expect(authorize).toHaveBeenCalledWith("/workspace/song.mp3");
  45. expect(fileService.readMediaPreview).not.toHaveBeenCalled();
  46. });
  47. it("uses bounded inline bytes for non-local media services", async () => {
  48. const fileService = createFileService();
  49. const service = createMediaPreviewService({ fileService, inlineMaxBytes: 8 });
  50. await expect(
  51. service.prepare({ path: "/workspace/song.mp3", expectedKind: "audio" }),
  52. ).resolves.toEqual({
  53. kind: "inline",
  54. dataBase64: "AQID",
  55. mediaType: "audio/mpeg",
  56. path: "/workspace/song.mp3",
  57. size: 3,
  58. });
  59. expect(fileService.readMediaPreview).toHaveBeenCalledWith({
  60. path: "/workspace/song.mp3",
  61. maxBytes: 8,
  62. });
  63. });
  64. it("rejects oversized non-local media without attempting a remote upload", async () => {
  65. const fileService = createFileService({
  66. stat: vi.fn(async () => ({ path: "/workspace/movie.mp4", type: "file", size: 9 })),
  67. });
  68. const service = createMediaPreviewService({ fileService, inlineMaxBytes: 8 });
  69. await expect(
  70. service.prepare({ path: "/workspace/movie.mp4", expectedKind: "video" }),
  71. ).rejects.toThrow(/too large for inline preview/);
  72. expect(fileService.readMediaPreview).not.toHaveBeenCalled();
  73. });
  74. it("rejects a kind mismatch before reading media bytes", async () => {
  75. const fileService = createFileService();
  76. const service = createMediaPreviewService({ fileService });
  77. await expect(
  78. service.prepare({ path: "/workspace/song.mp3", expectedKind: "video" }),
  79. ).rejects.toThrow(/Unsupported media preview format/);
  80. expect(fileService.stat).not.toHaveBeenCalled();
  81. });
  82. });