192 lines
5.8 KiB
TypeScript
192 lines
5.8 KiB
TypeScript
import type { DaemonClient } from "@server/client/daemon-client";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
clearWorkspaceArchivePending,
|
|
isWorkspaceArchivePending,
|
|
} from "@/contexts/session-workspace-upserts";
|
|
import { useSessionStore, type WorkspaceDescriptor } from "@/stores/session-store";
|
|
import {
|
|
archiveWorkspaceOptimistically,
|
|
archiveWorkspacesOptimistically,
|
|
type WorkspaceArchiveTarget,
|
|
} from "@/workspace/workspace-archive";
|
|
|
|
const SERVER_ID = "workspace-archive-test";
|
|
|
|
type ArchiveWorkspacePayload = Awaited<ReturnType<DaemonClient["archiveWorkspace"]>>;
|
|
|
|
function archivePayload(input: {
|
|
workspaceId: string;
|
|
error?: string | null;
|
|
}): ArchiveWorkspacePayload {
|
|
return {
|
|
requestId: "request",
|
|
workspaceId: input.workspaceId,
|
|
archivedAt: null,
|
|
error: input.error ?? null,
|
|
};
|
|
}
|
|
|
|
function workspace(input?: Partial<WorkspaceDescriptor>): WorkspaceDescriptor {
|
|
return {
|
|
id: "workspace-1",
|
|
projectId: "project-1",
|
|
projectDisplayName: "Project",
|
|
projectRootPath: "/repo/project",
|
|
workspaceDirectory: "/repo/project/workspace-1",
|
|
projectKind: "git",
|
|
workspaceKind: "worktree",
|
|
name: "workspace-1",
|
|
status: "done",
|
|
archivingAt: null,
|
|
diffStat: null,
|
|
scripts: [],
|
|
...input,
|
|
};
|
|
}
|
|
|
|
function target(input?: Partial<WorkspaceArchiveTarget>): WorkspaceArchiveTarget {
|
|
const base = workspace();
|
|
return {
|
|
serverId: SERVER_ID,
|
|
workspaceId: base.id,
|
|
workspaceDirectory: base.workspaceDirectory,
|
|
...input,
|
|
};
|
|
}
|
|
|
|
function createClient(
|
|
archiveWorkspace: DaemonClient["archiveWorkspace"],
|
|
): Pick<DaemonClient, "archiveWorkspace"> {
|
|
return { archiveWorkspace };
|
|
}
|
|
|
|
function deferred<T>(): {
|
|
promise: Promise<T>;
|
|
resolve: (value: T) => void;
|
|
reject: (error: unknown) => void;
|
|
} {
|
|
let resolve: (value: T) => void = () => {};
|
|
let reject: (error: unknown) => void = () => {};
|
|
const promise = new Promise<T>((promiseResolve, promiseReject) => {
|
|
resolve = promiseResolve;
|
|
reject = promiseReject;
|
|
});
|
|
return { promise, resolve, reject };
|
|
}
|
|
|
|
function storedWorkspace(id: string): WorkspaceDescriptor | undefined {
|
|
return useSessionStore.getState().sessions[SERVER_ID]?.workspaces.get(id);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
useSessionStore.getState().initializeSession(SERVER_ID, {} as DaemonClient);
|
|
});
|
|
|
|
afterEach(() => {
|
|
clearWorkspaceArchivePending({ serverId: SERVER_ID, workspaceId: "workspace-1" });
|
|
clearWorkspaceArchivePending({ serverId: SERVER_ID, workspaceId: "workspace-2" });
|
|
useSessionStore.setState((state) => ({ ...state, sessions: {} }));
|
|
});
|
|
|
|
describe("archiveWorkspaceOptimistically", () => {
|
|
it("hides the workspace and marks the archive pending while the daemon call runs", async () => {
|
|
const archived = workspace();
|
|
useSessionStore.getState().mergeWorkspaces(SERVER_ID, [archived]);
|
|
const releaseArchive = deferred<ArchiveWorkspacePayload>();
|
|
const client = createClient(vi.fn(async () => releaseArchive.promise));
|
|
|
|
const archive = archiveWorkspaceOptimistically({
|
|
client,
|
|
workspace: target(),
|
|
});
|
|
|
|
expect(storedWorkspace(archived.id)).toBeUndefined();
|
|
expect(
|
|
isWorkspaceArchivePending({
|
|
serverId: SERVER_ID,
|
|
workspaceId: archived.id,
|
|
workspaceDirectory: archived.workspaceDirectory,
|
|
}),
|
|
).toBe(true);
|
|
|
|
releaseArchive.resolve(archivePayload({ workspaceId: archived.id }));
|
|
await archive;
|
|
|
|
expect(storedWorkspace(archived.id)).toBeUndefined();
|
|
});
|
|
|
|
it("restores the workspace and clears pending state when the daemon rejects the archive", async () => {
|
|
const archived = workspace();
|
|
useSessionStore.getState().mergeWorkspaces(SERVER_ID, [archived]);
|
|
const client = createClient(
|
|
vi.fn(async () => archivePayload({ workspaceId: archived.id, error: "nope" })),
|
|
);
|
|
|
|
await expect(
|
|
archiveWorkspaceOptimistically({
|
|
client,
|
|
workspace: target(),
|
|
}),
|
|
).rejects.toThrow("nope");
|
|
|
|
expect(storedWorkspace(archived.id)).toEqual(archived);
|
|
expect(
|
|
isWorkspaceArchivePending({
|
|
serverId: SERVER_ID,
|
|
workspaceId: archived.id,
|
|
}),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("runs the after-hide hook after local state is hidden", async () => {
|
|
const archived = workspace();
|
|
useSessionStore.getState().mergeWorkspaces(SERVER_ID, [archived]);
|
|
const client = createClient(vi.fn(async () => archivePayload({ workspaceId: archived.id })));
|
|
const afterHide = vi.fn(() => {
|
|
expect(storedWorkspace(archived.id)).toBeUndefined();
|
|
});
|
|
|
|
await archiveWorkspaceOptimistically({
|
|
client,
|
|
workspace: target(),
|
|
afterHide,
|
|
});
|
|
|
|
expect(afterHide).toHaveBeenCalledOnce();
|
|
});
|
|
});
|
|
|
|
describe("archiveWorkspacesOptimistically", () => {
|
|
it("returns failures and restores only the workspaces whose archive failed", async () => {
|
|
const first = workspace({ id: "workspace-1" });
|
|
const second = workspace({
|
|
id: "workspace-2",
|
|
workspaceDirectory: "/repo/project/workspace-2",
|
|
name: "workspace-2",
|
|
});
|
|
useSessionStore.getState().mergeWorkspaces(SERVER_ID, [first, second]);
|
|
const client = createClient(
|
|
vi.fn(async (workspaceId) =>
|
|
archivePayload({
|
|
workspaceId,
|
|
error: workspaceId === second.id ? "failed" : null,
|
|
}),
|
|
),
|
|
);
|
|
|
|
const failures = await archiveWorkspacesOptimistically({
|
|
client,
|
|
workspaces: [
|
|
target({ workspaceId: first.id, workspaceDirectory: first.workspaceDirectory }),
|
|
target({ workspaceId: second.id, workspaceDirectory: second.workspaceDirectory }),
|
|
],
|
|
});
|
|
|
|
expect(failures).toHaveLength(1);
|
|
expect(failures[0]?.workspaceId).toBe(second.id);
|
|
expect(storedWorkspace(first.id)).toBeUndefined();
|
|
expect(storedWorkspace(second.id)).toEqual(second);
|
|
});
|
|
});
|