-
Notifications
You must be signed in to change notification settings - Fork 18
fix(frontend): show clear preview unavailable message for office documents #958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+180
−24
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b0291f
fix: show clear preview unavailable message for office documents
hieptl b778b6e
refactor: update the code based on feedback
hieptl a47b8ec
refactor: update the code based on feedback
hieptl 1747313
Merge branch 'main' into hieptl/app-2069
hieptl d91e9a5
Merge branch 'main' into hieptl/app-2069
hieptl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
__tests__/components/features/files-tab/file-content-viewer.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import React from "react"; | ||
| import { render, screen } from "@testing-library/react"; | ||
| import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { FileContentViewer } from "#/components/features/files-tab/file-content-viewer"; | ||
| import type { ViewMode } from "#/components/features/files-tab/view-mode"; | ||
| import { useWorkspaceMutationCounter } from "#/stores/use-workspace-mutation-counter"; | ||
|
|
||
| // Mock the *services* the file-content hook depends on — not the hook itself — | ||
| // so the real classification (text decoded, then flipped to binary on a NUL | ||
| // sniff) runs end to end through the viewer. | ||
| const useWorkspaceSessionMock = vi.fn(); | ||
| vi.mock("#/hooks/query/use-workspace-session", async (importOriginal) => { | ||
| const real = | ||
| await importOriginal< | ||
| typeof import("#/hooks/query/use-workspace-session") | ||
| >(); | ||
| return { | ||
| ...real, // keep the real joinWorkspaceUrl the hook builds its fetch URL with | ||
| useWorkspaceSession: () => useWorkspaceSessionMock(), | ||
| }; | ||
| }); | ||
|
|
||
| const useActiveConversationMock = vi.fn(); | ||
| vi.mock("#/hooks/query/use-active-conversation", () => ({ | ||
| useActiveConversation: () => useActiveConversationMock(), | ||
| })); | ||
|
|
||
| const useRuntimeIsReadyMock = vi.fn(); | ||
| vi.mock("#/hooks/use-runtime-is-ready", () => ({ | ||
| useRuntimeIsReady: () => useRuntimeIsReadyMock(), | ||
| })); | ||
|
|
||
| const getActiveBackendMock = vi.fn(); | ||
| vi.mock("#/api/backend-registry/active-store", () => ({ | ||
| getActiveBackend: () => getActiveBackendMock(), | ||
| })); | ||
|
|
||
| // The hook statically imports the cloud runtime service; stub the module so | ||
| // this local-path test never loads the real cloud/proxy machinery. The test | ||
| // uses the fetch (local) path, so downloadFile is never called or asserted. | ||
| vi.mock("#/api/runtime-service/agent-server-runtime-service", () => ({ | ||
| default: { downloadFile: vi.fn() }, | ||
| })); | ||
|
|
||
| const fetchMock = vi.fn(); | ||
|
|
||
| const BASE_URL = | ||
| "https://agent.example.com/api/conversations/conv-1/workspace/"; | ||
|
|
||
| function renderViewer(path: string, viewMode: ViewMode = "rich") { | ||
| const client = new QueryClient({ | ||
| defaultOptions: { queries: { retry: false } }, | ||
| }); | ||
| return render( | ||
| <QueryClientProvider client={client}> | ||
| <FileContentViewer path={path} viewMode={viewMode} /> | ||
| </QueryClientProvider>, | ||
| ); | ||
| } | ||
|
|
||
| describe("FileContentViewer", () => { | ||
| beforeEach(() => { | ||
| vi.stubGlobal("fetch", fetchMock); | ||
| fetchMock.mockReset(); | ||
| useWorkspaceSessionMock.mockReset(); | ||
| useActiveConversationMock.mockReset(); | ||
| useRuntimeIsReadyMock.mockReset(); | ||
| getActiveBackendMock.mockReset(); | ||
|
|
||
| useRuntimeIsReadyMock.mockReturnValue(true); | ||
| useActiveConversationMock.mockReturnValue({ | ||
| data: { | ||
| id: "conv-1", | ||
| conversation_url: "https://agent.example.com/api/conversations/conv-1", | ||
| session_api_key: "session-key", | ||
| }, | ||
| }); | ||
| useWorkspaceSessionMock.mockReturnValue({ | ||
| data: { baseUrl: BASE_URL }, | ||
| isLoading: false, | ||
| isError: false, | ||
| error: null, | ||
| }); | ||
| getActiveBackendMock.mockReturnValue({ | ||
| backend: { id: "local-1", kind: "local", host: "http://localhost:8000" }, | ||
| orgId: null, | ||
| }); | ||
| useWorkspaceMutationCounter.setState({ count: 0 }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| // The acceptance criteria require the clear message in BOTH view modes. The | ||
| // plain-mode fallback and the rich-mode binary branch both route through | ||
| // UnpreviewableFallback, so one parametrized spec covers both code paths. | ||
| it.each(["rich", "plain"] as const)( | ||
| "shows a clear unsupported-document message for an Office file (.pptx) in %s mode", | ||
| async (viewMode) => { | ||
| // Arrange: the workspace fileserver returns real .pptx bytes — a ZIP whose | ||
| // header carries a NUL, so the hook classifies the file as binary. | ||
| fetchMock.mockResolvedValue({ | ||
| ok: true, | ||
| status: 200, | ||
| arrayBuffer: () => | ||
| Promise.resolve( | ||
| new Uint8Array([0x50, 0x4b, 0x03, 0x04, 0x00]).buffer, | ||
| ), | ||
| }); | ||
|
|
||
| // Act | ||
| renderViewer("demo.pptx", viewMode); | ||
|
|
||
| // Assert: the format-aware "no preview" message replaces the generic | ||
| // binary fallback in both modes, so the pane is never blank. | ||
| expect( | ||
| await screen.findByTestId("file-content-viewer-unsupported-document"), | ||
| ).toBeInTheDocument(); | ||
| }, | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.