Files
kandev/apps/web/hooks/use-file-editors.build-state.test.ts
T
José Almeida 214a8f5cce fix: scope multi-repo file editor open/save to the right repo (#1382)
* fix: scope multi-repo file editor open/save to the right repo

The editor open/save/sync/delete chain dropped the repository_name in
multi-repo task workspaces, so the backend resolved paths against the
bare task root and failed with "file not found". Thread the repo
subpath through openFile, save, sync, and delete, and store it on
FileEditorState.

* fix: pass repo from diff Edit button to multi-repo file editor

The diff/changes "Edit" button opened files without their repository
scope, so multi-repo files resolved against the bare task root and
failed with "file not found". Thread repository_name through the diff
and review toolbars into onOpenFile.

* fix: preserve repo across tab restore and stale syncs

Address review feedback: persisted file tabs dropped the repo subpath,
so a multi-repo file reopened against the bare task root after a browser
refresh. Persist and restore `repo` on StoredFileTab. Also guard the
workspace sync against a tab being swapped to a different repo mid-fetch,
and widen the remaining onOpenFile type declarations to carry repo.

* fix: thread repo into editor panel fallback fetch

Address review feedback: FileEditorPanel's useFileLoader fallback fetch
(triggered when the open-files map is transiently empty, e.g. after a
session switch) called requestFileContent without the repo subpath,
re-introducing the multi-repo "file not found" failure. Record repo on
the panel params and forward it through useFileLoader. Also assert the
full restored-tab shape in the local-storage round-trip test.

* fix: guard editor fetches against stale session/repo races

Address review feedback: file open and the panel fallback fetch could
apply a late response after the user navigated away. openFile now drops
the result if the active session changed mid-fetch (via fetchFileEditorState),
and the panel loader keys its in-flight guard by session+path+repo so a
changed target re-fetches and stale responses are discarded.
2026-06-15 17:59:05 +01:00

76 lines
2.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import type { FileContentResponse } from "@/lib/types/backend";
const mockRequestFileContent = vi.fn();
vi.mock("@/lib/utils/file-diff", () => ({
calculateHash: async (s: string) => `h:${s.length}`,
}));
vi.mock("@/lib/ws/workspace-files", () => ({
requestFileContent: (...args: unknown[]) => mockRequestFileContent(...args),
}));
vi.mock("@/lib/ws/connection", () => ({
getWebSocketClient: () => ({}),
}));
import { buildFileEditorState, fetchFileEditorState } from "./use-file-editors";
const PATH = "src/foo.ts";
const REPO = "enrichment-commons";
const SESSION_ID = "sess-1";
const RESPONSE: FileContentResponse = {
path: PATH,
content: "v1",
is_binary: false,
} as FileContentResponse;
const FAKE_CLIENT = {} as NonNullable<
ReturnType<typeof import("@/lib/ws/connection").getWebSocketClient>
>;
describe("buildFileEditorState", () => {
it("carries the repo subpath so subsequent save/sync calls scope to the right repository", async () => {
// Multi-repo open: opening foo.ts from the "enrichment-commons" repo must
// record `repo` on the editor state, otherwise later save/sync requests
// drop it and the backend stats the bare task root → "file not found".
const state = await buildFileEditorState(PATH, RESPONSE, REPO);
expect(state.repo).toBe(REPO);
});
it("leaves repo undefined for single-repo tasks", async () => {
const state = await buildFileEditorState(PATH, RESPONSE);
expect(state.repo).toBeUndefined();
});
});
describe("fetchFileEditorState", () => {
beforeEach(() => vi.clearAllMocks());
it("returns the built state (with repo) when the session is unchanged", async () => {
mockRequestFileContent.mockResolvedValueOnce(RESPONSE);
const ref = { current: SESSION_ID };
const state = await fetchFileEditorState(FAKE_CLIENT, SESSION_ID, PATH, REPO, ref);
expect(mockRequestFileContent).toHaveBeenCalledWith(FAKE_CLIENT, SESSION_ID, PATH, REPO);
expect(state?.repo).toBe(REPO);
});
it("returns null when the active session changed while the fetch was in flight", async () => {
// User switches tasks mid-fetch: the late response must not be applied to
// the new session's editor state.
const ref = { current: SESSION_ID };
mockRequestFileContent.mockImplementationOnce(async () => {
ref.current = "sess-2";
return RESPONSE;
});
const state = await fetchFileEditorState(FAKE_CLIENT, SESSION_ID, PATH, undefined, ref);
expect(state).toBeNull();
});
});