refactor: transition to opaque workspace identifiers
This commit is contained in:
@@ -5,6 +5,7 @@ import { patchWorkspaceScripts } from "./session-workspace-scripts";
|
||||
|
||||
function workspace(input: {
|
||||
id: string;
|
||||
workspaceDirectory?: string;
|
||||
scripts?: WorkspaceDescriptor["scripts"];
|
||||
}): WorkspaceDescriptor {
|
||||
return {
|
||||
@@ -12,7 +13,7 @@ function workspace(input: {
|
||||
projectId: "project-1",
|
||||
projectDisplayName: "Project 1",
|
||||
projectRootPath: "/repo",
|
||||
workspaceDirectory: input.id,
|
||||
workspaceDirectory: input.workspaceDirectory ?? "/repo/main",
|
||||
projectKind: "git",
|
||||
workspaceKind: "checkout",
|
||||
name: "main",
|
||||
@@ -35,58 +36,54 @@ const runningScript: WorkspaceScriptPayload = {
|
||||
|
||||
describe("patchWorkspaceScripts", () => {
|
||||
it("patches only the matching workspace scripts", () => {
|
||||
const other = workspace({ id: "/repo/other", scripts: [] });
|
||||
const other = workspace({ id: "ws-other", workspaceDirectory: "/repo/other", scripts: [] });
|
||||
const current = new Map<string, WorkspaceDescriptor>([
|
||||
["/repo/main", workspace({ id: "/repo/main", scripts: [] })],
|
||||
["ws-main", workspace({ id: "ws-main", workspaceDirectory: "/repo/main", scripts: [] })],
|
||||
[other.id, other],
|
||||
]);
|
||||
|
||||
const next = patchWorkspaceScripts(current, {
|
||||
workspaceId: "/repo/main",
|
||||
workspaceId: "ws-main",
|
||||
scripts: [runningScript],
|
||||
});
|
||||
|
||||
expect(next).not.toBe(current);
|
||||
expect(next.get("/repo/main")?.scripts).toEqual([runningScript]);
|
||||
expect(next.get("/repo/other")).toBe(other);
|
||||
expect(next.get("ws-main")?.scripts).toEqual([runningScript]);
|
||||
expect(next.get("ws-other")).toBe(other);
|
||||
});
|
||||
|
||||
it("patches the matching workspace when the update uses workspace directory identity", () => {
|
||||
it("patches the matching workspace when the map key differs from the workspace id", () => {
|
||||
const current = new Map<string, WorkspaceDescriptor>([
|
||||
[
|
||||
"42",
|
||||
"workspace-record-42",
|
||||
workspace({
|
||||
id: "42",
|
||||
id: "ws-main",
|
||||
workspaceDirectory: "C:\\repo\\main\\",
|
||||
scripts: [],
|
||||
}),
|
||||
],
|
||||
]);
|
||||
|
||||
current.set("42", {
|
||||
...current.get("42")!,
|
||||
workspaceDirectory: "C:\\repo\\main\\",
|
||||
});
|
||||
|
||||
const next = patchWorkspaceScripts(current, {
|
||||
workspaceId: "C:/repo/main",
|
||||
workspaceId: "ws-main",
|
||||
scripts: [runningScript],
|
||||
});
|
||||
|
||||
expect(next).not.toBe(current);
|
||||
expect(next.get("42")?.scripts).toEqual([runningScript]);
|
||||
expect(next.get("workspace-record-42")?.scripts).toEqual([runningScript]);
|
||||
});
|
||||
|
||||
it("ignores updates for unknown workspaces", () => {
|
||||
const current = new Map<string, WorkspaceDescriptor>([
|
||||
["/repo/main", workspace({ id: "/repo/main", scripts: [] })],
|
||||
["ws-main", workspace({ id: "ws-main", workspaceDirectory: "/repo/main", scripts: [] })],
|
||||
]);
|
||||
|
||||
const next = patchWorkspaceScripts(current, {
|
||||
workspaceId: "/repo/missing",
|
||||
workspaceId: "ws-missing",
|
||||
scripts: [runningScript],
|
||||
});
|
||||
|
||||
expect(next).toBe(current);
|
||||
expect(next.get("/repo/main")?.scripts).toEqual([]);
|
||||
expect(next.get("ws-main")?.scripts).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,7 +8,7 @@ export function patchWorkspaceScripts(
|
||||
): Map<string, WorkspaceDescriptor> {
|
||||
const workspaceKey = resolveWorkspaceMapKeyByIdentity({
|
||||
workspaces,
|
||||
workspaceIdentity: update.workspaceId,
|
||||
workspaceId: update.workspaceId,
|
||||
});
|
||||
if (!workspaceKey) {
|
||||
return workspaces;
|
||||
|
||||
Reference in New Issue
Block a user