Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions frontend/editor/src/core/hooks/useFileManager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { renderHook, act } from "@testing-library/react";
import { useFileManager } from "@app/hooks/useFileManager";
import apiClient from "@app/services/apiClient";

vi.mock("@app/services/apiClient", () => ({
default: { get: vi.fn() },
}));
vi.mock("@app/contexts/IndexedDBContext", () => ({
useIndexedDB: () => ({}),
}));
vi.mock("@app/services/fileStorage", () => ({
fileStorage: { getLeafStirlingFileStubs: vi.fn(async () => []) },
}));
vi.mock("@app/services/pruneMissingRecentFiles", () => ({
pruneMissingRecentFiles: vi.fn(async (stubs: unknown[]) => stubs),
}));
vi.mock("@app/hooks/useDiskLinkReconcile", () => ({
useDiskLinkReconcile: () => ({
openFileIdsRef: { current: [] },
onOpenFilesDetached: vi.fn(),
}),
}));
vi.mock("@app/contexts/AppConfigContext", () => ({
useAppConfig: () => ({
config: { storageEnabled: true, storageShareLinksEnabled: true },
}),
}));
const authState = vi.hoisted(() => ({ isAnonymous: false }));
vi.mock("@app/auth/UseSession", () => ({ useAuth: () => authState }));

const mockGet = vi.mocked(apiClient.get);

describe("useFileManager server files", () => {
beforeEach(() => {
vi.clearAllMocks();
authState.isAnonymous = false;
mockGet.mockResolvedValue({ data: [] });
});

it("does not ask the server for a guest's stored files", async () => {
authState.isAnonymous = true;
const { result } = renderHook(() => useFileManager());

await act(async () => {
await result.current.loadRecentFiles();
});

expect(mockGet).not.toHaveBeenCalled();
});

it("loads stored files and accessed share links for an account", async () => {
const { result } = renderHook(() => useFileManager());

await act(async () => {
await result.current.loadRecentFiles();
});

const urls = mockGet.mock.calls.map(([url]) => url);
expect(urls).toEqual([
"/api/v1/storage/files",
"/api/v1/storage/share-links/accessed",
]);
});
});
7 changes: 6 additions & 1 deletion frontend/editor/src/core/hooks/useFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { StirlingFileStub, StirlingFile } from "@app/types/fileContext";
import { FileId } from "@app/types/fileContext";
import apiClient from "@app/services/apiClient";
import { useAppConfig } from "@app/contexts/AppConfigContext";
import { useAuth } from "@app/auth/UseSession";
import { useDiskLinkReconcile } from "@app/hooks/useDiskLinkReconcile";

interface StoredFileResponse {
Expand Down Expand Up @@ -37,6 +38,7 @@ export const useFileManager = () => {
const [loading, setLoading] = useState(false);
const indexedDB = useIndexedDB();
const { config } = useAppConfig();
const { isAnonymous } = useAuth();

// Refs inside, so loadRecentFiles isn't recreated on every workbench change -
// its consumers re-run it on identity change, which would loop.
Expand Down Expand Up @@ -125,7 +127,9 @@ export const useFileManager = () => {
);
let combinedStubs = stirlingFileStubs;

const shouldFetchServerFiles = config?.storageEnabled === true;
// Guests have no server storage; the request would only 401.
const shouldFetchServerFiles =
config?.storageEnabled === true && !isAnonymous;
Comment thread
jbrunton96 marked this conversation as resolved.
Resolved

if (shouldFetchServerFiles) {
try {
Expand Down Expand Up @@ -380,6 +384,7 @@ export const useFileManager = () => {
config?.enableLogin,
config?.storageEnabled,
config?.storageShareLinksEnabled,
isAnonymous,
normalizeServerFileName,
openFileIdsRef,
onOpenFilesDetached,
Expand Down
Loading