| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestSwitchWorkspaceReusesInitialConversation(t *testing.T) { |
| 10 | isolateDesktopUserDirs(t) |
| 11 | a := NewApp() |
| 12 | installNoopRuntimeEvents(a) |
| 13 | root, other := t.TempDir(), t.TempDir() |
| 14 | t.Cleanup(func() { a.shutdown(context.Background()) }) |
| 15 | for _, dir := range []string{root, root, other, root} { |
| 16 | if _, err := a.SwitchWorkspace(dir); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | } |
| 20 | if topics := loadTopicTitles(root); len(topics) != 1 { |
| 21 | t.Fatalf("opening/retrying the same workspace created %d conversations: %v", len(topics), topics) |
| 22 | } |
| 23 | a.shutdown(context.Background()) |
| 24 | reopened := NewApp() |
| 25 | installNoopRuntimeEvents(reopened) |
| 26 | t.Cleanup(func() { reopened.shutdown(context.Background()) }) |
| 27 | if _, err := reopened.SwitchWorkspace(root); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if topics := loadTopicTitles(root); len(topics) != 1 { |
| 31 | t.Fatalf("reopening after restart created another conversation: %v", topics) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestSwitchWorkspaceConcurrentOpenCreatesOneConversation(t *testing.T) { |
| 36 | isolateDesktopUserDirs(t) |
| 37 | a := NewApp() |
| 38 | installNoopRuntimeEvents(a) |
| 39 | root := t.TempDir() |
| 40 | t.Cleanup(func() { a.shutdown(context.Background()) }) |
| 41 | var wg sync.WaitGroup |
| 42 | for range 2 { |
| 43 | wg.Go(func() { _, _ = a.SwitchWorkspace(root) }) |
| 44 | } |
| 45 | wg.Wait() |
| 46 | if topics := loadTopicTitles(root); len(topics) != 1 { |
| 47 | t.Fatalf("concurrent workspace open created %d conversations: %v", len(topics), topics) |
| 48 | } |
| 49 | } |
| 50 |