Files
kandev/apps/web/lib/local-storage.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

173 lines
4.8 KiB
TypeScript

import { beforeEach, describe, expect, it } from "vitest";
import {
cleanupTaskStorage,
clearGlobalSidebarWidth,
getGlobalSidebarWidth,
getOpenFileTabs,
markPRClosedBannerDismissed,
markPRMergedBannerDismissed,
restoreAttachmentPreview,
setGlobalSidebarWidth,
setOpenFileTabs,
wasPRClosedBannerDismissed,
wasPRMergedBannerDismissed,
} from "./local-storage";
describe("PR merged banner dismissal storage", () => {
beforeEach(() => {
window.sessionStorage.clear();
});
it("returns false when no dismissal has been recorded", () => {
expect(wasPRMergedBannerDismissed("task-a")).toBe(false);
});
it("persists dismissal per task and reads it back", () => {
markPRMergedBannerDismissed("task-a");
expect(wasPRMergedBannerDismissed("task-a")).toBe(true);
expect(wasPRMergedBannerDismissed("task-b")).toBe(false);
});
it("clears the dismissal flag via cleanupTaskStorage", () => {
markPRMergedBannerDismissed("task-a");
markPRMergedBannerDismissed("task-b");
cleanupTaskStorage("task-a", []);
expect(wasPRMergedBannerDismissed("task-a")).toBe(false);
expect(wasPRMergedBannerDismissed("task-b")).toBe(true);
});
});
describe("PR closed banner dismissal storage", () => {
beforeEach(() => {
window.sessionStorage.clear();
});
it("returns false when no dismissal has been recorded", () => {
expect(wasPRClosedBannerDismissed("task-a")).toBe(false);
});
it("persists dismissal per task and reads it back", () => {
markPRClosedBannerDismissed("task-a");
expect(wasPRClosedBannerDismissed("task-a")).toBe(true);
expect(wasPRClosedBannerDismissed("task-b")).toBe(false);
});
it("is independent from the merged banner dismissal flag", () => {
markPRMergedBannerDismissed("task-a");
expect(wasPRClosedBannerDismissed("task-a")).toBe(false);
});
it("clears the dismissal flag via cleanupTaskStorage", () => {
markPRClosedBannerDismissed("task-a");
markPRClosedBannerDismissed("task-b");
cleanupTaskStorage("task-a", []);
expect(wasPRClosedBannerDismissed("task-a")).toBe(false);
expect(wasPRClosedBannerDismissed("task-b")).toBe(true);
});
});
describe("global sidebar width storage", () => {
beforeEach(() => {
window.localStorage.clear();
});
it("returns null when no width has been stored", () => {
expect(getGlobalSidebarWidth()).toBeNull();
});
it("persists a width globally and reads it back (rounded)", () => {
setGlobalSidebarWidth(412.6);
expect(getGlobalSidebarWidth()).toBe(413);
});
it("ignores non-positive or non-finite widths", () => {
setGlobalSidebarWidth(0);
setGlobalSidebarWidth(-100);
setGlobalSidebarWidth(Number.NaN);
expect(getGlobalSidebarWidth()).toBeNull();
});
it("clears the stored width", () => {
setGlobalSidebarWidth(320);
clearGlobalSidebarWidth();
expect(getGlobalSidebarWidth()).toBeNull();
});
it("is NOT removed by cleanupTaskStorage (it is global, not task-scoped)", () => {
setGlobalSidebarWidth(320);
cleanupTaskStorage("task-a", []);
expect(getGlobalSidebarWidth()).toBe(320);
});
});
describe("open file tabs storage", () => {
beforeEach(() => {
window.sessionStorage.clear();
});
it("round-trips the multi-repo repo subpath so a restored tab refetches under the right repo", () => {
setOpenFileTabs("sess-1", [
{
path: "src/foo.ts",
name: "foo.ts",
repo: "enrichment-commons",
markdownPreview: true,
pinned: true,
},
]);
const tabs = getOpenFileTabs("sess-1");
expect(tabs).toHaveLength(1);
expect(tabs[0]).toEqual({
path: "src/foo.ts",
name: "foo.ts",
repo: "enrichment-commons",
markdownPreview: true,
pinned: true,
});
});
it("leaves repo undefined for single-repo tabs", () => {
setOpenFileTabs("sess-1", [{ path: "src/foo.ts", name: "foo.ts", pinned: true }]);
expect(getOpenFileTabs("sess-1")[0].repo).toBeUndefined();
});
});
describe("chat draft attachment storage", () => {
it("normalizes invalid restored image delivery modes to prompt", () => {
const restored = restoreAttachmentPreview({
id: "att-1",
data: "abc",
mimeType: "image/png",
fileName: "shot.png",
size: 3,
isImage: true,
deliveryMode: "inline" as "prompt",
});
expect(restored.deliveryMode).toBe("prompt");
expect(restored.preview).toBe("data:image/png;base64,abc");
});
it("normalizes invalid restored file delivery modes to path", () => {
const restored = restoreAttachmentPreview({
id: "att-2",
data: "abc",
mimeType: "application/pdf",
fileName: "doc.pdf",
size: 3,
isImage: false,
deliveryMode: "inline" as "path",
});
expect(restored.deliveryMode).toBe("path");
});
});