| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/config" |
| 8 | "reasonix/internal/session" |
| 9 | ) |
| 10 | |
| 11 | func TestNewAppPinsDesktopV5SessionRootBeforeStartup(t *testing.T) { |
| 12 | isolateDesktopUserDirs(t) |
| 13 | app := NewApp() |
| 14 | t.Cleanup(app.closeSessionServices) |
| 15 | |
| 16 | legacyDir := filepath.Join(t.TempDir(), "project", "sessions") |
| 17 | if got, want := app.desktopSessions.root, config.DesktopSessionStoreDir(); !sameDesktopPath(got, want) { |
| 18 | t.Fatalf("desktop session root = %q, want v5 root %q", got, want) |
| 19 | } |
| 20 | if sameDesktopPath(app.desktopSessions.root, desktopSessionRoot(legacyDir)) { |
| 21 | t.Fatal("desktop App regressed to a legacy-derived canonical root before startup") |
| 22 | } |
| 23 | if app.desktopSessionService(legacyDir) == nil { |
| 24 | t.Fatal("desktop session service is nil") |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // pinDesktopSessionRoot points the canonical store at a disposable root and |
| 29 | // registers the service close after that root's own RemoveAll. t.Cleanup is |
| 30 | // LIFO, so registering in the other order removes the directory while the |
| 31 | // service still holds its writer lease and recovery database: POSIX allows |
| 32 | // unlinking open files, Windows fails the test during cleanup. |
| 33 | func pinDesktopSessionRoot(t *testing.T, app *App) string { |
| 34 | t.Helper() |
| 35 | root := filepath.Join(t.TempDir(), "desktop-sessions-v5", "by-id") |
| 36 | app.desktopSessions.root = root |
| 37 | t.Cleanup(app.closeSessionServices) |
| 38 | return root |
| 39 | } |
| 40 | |
| 41 | func TestDesktopSessionServiceIsSharedAcrossWorkspaces(t *testing.T) { |
| 42 | app := NewApp() |
| 43 | pinDesktopSessionRoot(t, app) |
| 44 | |
| 45 | first := app.desktopSessionService(filepath.Join(t.TempDir(), "project-a", "sessions")) |
| 46 | second := app.desktopSessionService(filepath.Join(t.TempDir(), "project-b", "sessions")) |
| 47 | if first == nil || second == nil { |
| 48 | t.Fatal("desktop session service is nil") |
| 49 | } |
| 50 | if first != second { |
| 51 | t.Fatal("workspace directories selected different canonical services") |
| 52 | } |
| 53 | |
| 54 | workspaceRoot := filepath.Join(t.TempDir(), "project-a") |
| 55 | runtime, err := first.Create(t.Context(), session.CreateOptions{SessionID: "shared-session", CWD: workspaceRoot, Origin: session.SessionOriginNew}) |
| 56 | if err != nil { |
| 57 | t.Fatalf("Create: %v", err) |
| 58 | } |
| 59 | if _, err := runtime.Session().Flush(t.Context()); err != nil { |
| 60 | t.Fatalf("Flush: %v", err) |
| 61 | } |
| 62 | page, err := second.Query().List(t.Context(), "", 10) |
| 63 | if err != nil { |
| 64 | t.Fatalf("List through second workspace: %v", err) |
| 65 | } |
| 66 | if len(page.Sessions) != 1 || !sameDesktopPath(page.Sessions[0].CWD, workspaceRoot) { |
| 67 | t.Fatalf("sessions = %#v", page.Sessions) |
| 68 | } |
| 69 | } |
| 70 |