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