| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func sharedHostRefsForTest(t *testing.T, app *App, root string) (int, bool) { |
| 9 | t.Helper() |
| 10 | if app == nil { |
| 11 | return 0, false |
| 12 | } |
| 13 | app.sharedHostsMu.Lock() |
| 14 | defer app.sharedHostsMu.Unlock() |
| 15 | if app.sharedHosts == nil { |
| 16 | return 0, false |
| 17 | } |
| 18 | entry, ok := app.sharedHosts[root] |
| 19 | if !ok || entry == nil { |
| 20 | return 0, false |
| 21 | } |
| 22 | return entry.refs, true |
| 23 | } |
| 24 | |
| 25 | func TestCloneDetachedRuntimeTabPreservesSharedHostKey(t *testing.T) { |
| 26 | tab := &WorkspaceTab{ID: "tab", SharedHostKey: "workspace-root"} |
| 27 | detached := cloneDetachedRuntimeTab(tab, "session-key", "session-key") |
| 28 | if detached == nil { |
| 29 | t.Fatal("cloneDetachedRuntimeTab returned nil") |
| 30 | } |
| 31 | if detached.SharedHostKey != tab.SharedHostKey { |
| 32 | t.Fatalf("detached SharedHostKey = %q, want %q", detached.SharedHostKey, tab.SharedHostKey) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestApplyRuntimeTabTransfersDetachedSharedHostKey(t *testing.T) { |
| 37 | target := &WorkspaceTab{ID: "visible", SharedHostKey: "new-build-ref"} |
| 38 | source := &WorkspaceTab{ID: "detached", SharedHostKey: "detached-ref"} |
| 39 | |
| 40 | applyRuntimeTab(target, source, "session-key", context.Background(), NewApp()) |
| 41 | |
| 42 | if target.SharedHostKey != "detached-ref" { |
| 43 | t.Fatalf("target SharedHostKey = %q, want detached-ref", target.SharedHostKey) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestCloseRemovedSessionRuntimesReleasesSharedHost(t *testing.T) { |
| 48 | app := NewApp() |
| 49 | root := "workspace-root" |
| 50 | app.acquireSharedHost(root) |
| 51 | |
| 52 | tab := &WorkspaceTab{ID: "tab", SharedHostKey: root} |
| 53 | app.closeRemovedSessionRuntimes([]removedSessionRuntime{{tab: tab}}) |
| 54 | |
| 55 | if tab.SharedHostKey != "" { |
| 56 | t.Fatalf("tab SharedHostKey = %q, want cleared", tab.SharedHostKey) |
| 57 | } |
| 58 | if refs, ok := sharedHostRefsForTest(t, app, root); ok { |
| 59 | t.Fatalf("shared host still present with refs=%d, want released", refs) |
| 60 | } |
| 61 | } |
| 62 |