| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "reasonix/desktop/internal/workspacestate" |
| 7 | ) |
| 8 | |
| 9 | func TestRemoveWorkspaceDropsVisibleTabsAndPersistedEntries(t *testing.T) { |
| 10 | isolateDesktopUserDirs(t) |
| 11 | projectRoot := t.TempDir() |
| 12 | if err := addProject(projectRoot, "Project"); err != nil { |
| 13 | t.Fatalf("add project: %v", err) |
| 14 | } |
| 15 | app := &App{ |
| 16 | tabs: map[string]*WorkspaceTab{ |
| 17 | "project": {ID: "project", Scope: "project", WorkspaceRoot: projectRoot, TopicID: "topic-project", Ready: true, disabledMCP: map[string]ServerView{}}, |
| 18 | "global": {ID: "global", Scope: "global", WorkspaceRoot: globalTabWorkspaceRoot(), TopicID: "topic-global", Ready: true, disabledMCP: map[string]ServerView{}}, |
| 19 | }, |
| 20 | tabOrder: []string{"project", "global"}, |
| 21 | activeTabID: "project", |
| 22 | detachedSessions: map[string]*WorkspaceTab{}, |
| 23 | } |
| 24 | if err := app.workspaceRegistry().EnsureWorkspace(t.Context(), workspacestate.Workspace{ |
| 25 | ID: "preserved-owner", Root: projectRoot, Title: "Project", Visible: true, |
| 26 | }); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | app.mu.Lock() |
| 30 | app.saveTabsLocked() |
| 31 | app.mu.Unlock() |
| 32 | |
| 33 | if err := app.RemoveWorkspace(projectRoot); err != nil { |
| 34 | t.Fatalf("RemoveWorkspace: %v", err) |
| 35 | } |
| 36 | assertTabIDs(t, app.ListTabs(), "global") |
| 37 | if got := app.ListWorkspaces(); len(got) != 0 { |
| 38 | t.Fatalf("workspaces after remove = %+v, want none", got) |
| 39 | } |
| 40 | if got := loadTabsFile(); len(got.Tabs) != 1 || got.Tabs[0].ID != "global" { |
| 41 | t.Fatalf("persisted tabs after workspace remove = %+v, want only global", got) |
| 42 | } |
| 43 | state, err := app.workspaceRegistry().Load(t.Context()) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if state.Workspaces["preserved-owner"].Visible { |
| 48 | t.Fatal("removed physical workspace remained visible in the authoritative registry") |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestMergeCanonicalWorkspaceShellsUsesPersistedPhysicalOwner(t *testing.T) { |
| 53 | isolateDesktopUserDirs(t) |
| 54 | app := NewApp() |
| 55 | app.ctx = t.Context() |
| 56 | root := t.TempDir() |
| 57 | |
| 58 | if err := app.workspaceRegistry().EnsureWorkspace(t.Context(), workspacestate.Workspace{ |
| 59 | ID: "preserved-owner", Root: root, Title: "新建文件夹", Visible: true, |
| 60 | }); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | |
| 64 | legacy := ProjectNode{Key: "project_" + root, Kind: "project", Root: root, Label: "Projects"} |
| 65 | got := app.mergeCanonicalWorkspaceShells([]ProjectNode{legacy}) |
| 66 | if len(got) != 1 { |
| 67 | t.Fatalf("one physical workspace rendered %d sidebar nodes: %+v", len(got), got) |
| 68 | } |
| 69 | if got[0].Key != legacy.Key || got[0].Label != legacy.Label { |
| 70 | t.Fatalf("legacy presentation was replaced: got=%+v want=%+v", got[0], legacy) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestMergeCanonicalWorkspaceShellsHonorsPersistedOwnerVisibility(t *testing.T) { |
| 75 | isolateDesktopUserDirs(t) |
| 76 | app := NewApp() |
| 77 | app.ctx = t.Context() |
| 78 | root := t.TempDir() |
| 79 | |
| 80 | if err := app.workspaceRegistry().EnsureWorkspace(t.Context(), workspacestate.Workspace{ |
| 81 | ID: "preserved-owner", Root: root, Title: "Hidden", Visible: false, |
| 82 | }); err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | |
| 86 | legacy := ProjectNode{Key: "project_" + root, Kind: "project", Root: root, Label: "Projects"} |
| 87 | if got := app.mergeCanonicalWorkspaceShells([]ProjectNode{legacy}); len(got) != 0 { |
| 88 | t.Fatalf("hidden physical workspace remained visible: %+v", got) |
| 89 | } |
| 90 | } |
| 91 |