| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/session" |
| 8 | ) |
| 9 | |
| 10 | func TestNewBlankSessionCanBeArchived(t *testing.T) { |
| 11 | for _, scope := range []string{"global", "project"} { |
| 12 | t.Run(scope, func(t *testing.T) { |
| 13 | isolateDesktopUserDirs(t) |
| 14 | a := NewApp() |
| 15 | a.ctx = t.Context() |
| 16 | installNoopRuntimeEvents(a) |
| 17 | t.Cleanup(a.closeSessionServices) |
| 18 | root := "" |
| 19 | if scope == "project" { |
| 20 | root = t.TempDir() |
| 21 | } |
| 22 | meta, err := a.EnsureBlankTab(scope, root) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | ref := session.SessionRef{HostID: localDesktopHostID, SessionID: meta.SessionID} |
| 27 | result, err := a.ArchiveSessionTarget(SessionSelector{Ref: &ref}) |
| 28 | if err != nil || !result.Committed { |
| 29 | t.Fatalf("archive new blank session: %+v, %v", result, err) |
| 30 | } |
| 31 | assertNoVisibleRuntime(t, a) |
| 32 | page, err := a.ListProjectTopics(ProjectTopicPageRequest{Scope: scope, WorkspaceRoot: root, Limit: 50}) |
| 33 | if err != nil || len(page.Items) != 0 { |
| 34 | t.Fatalf("archived blank remains in sidebar: %+v, %v", page, err) |
| 35 | } |
| 36 | }) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestNewUnopenedTopicCanBeArchived(t *testing.T) { |
| 41 | for _, scope := range []string{"global", "project"} { |
| 42 | t.Run(scope, func(t *testing.T) { |
| 43 | isolateDesktopUserDirs(t) |
| 44 | a := NewApp() |
| 45 | a.ctx = t.Context() |
| 46 | installNoopRuntimeEvents(a) |
| 47 | t.Cleanup(a.closeSessionServices) |
| 48 | root := "" |
| 49 | if scope == "project" { |
| 50 | root = t.TempDir() |
| 51 | } |
| 52 | topic, err := a.CreateTopic(scope, root, "") |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := a.TrashTopic(topic.ID); err != nil { |
| 57 | t.Fatalf("archive unopened blank topic: %v", err) |
| 58 | } |
| 59 | assertNoVisibleRuntime(t, a) |
| 60 | if topicIndexedInRegistry(scope, root, topic.ID) || loadTopicTitle(root, topic.ID) != "" { |
| 61 | t.Fatal("archived topic remains in metadata") |
| 62 | } |
| 63 | if !containsDesktopString(loadProjectsFile().DeletedTopics, topic.ID) { |
| 64 | t.Fatal("missing tombstone for archived empty topic") |
| 65 | } |
| 66 | if _, err := os.Stat(topicArchiveMetadataPendingPath(topic.ID)); !os.IsNotExist(err) { |
| 67 | t.Fatalf("metadata cleanup remains pending: %v", err) |
| 68 | } |
| 69 | // A fresh application must not reconstruct the row from legacy metadata. |
| 70 | restarted := NewApp() |
| 71 | restarted.ctx = t.Context() |
| 72 | t.Cleanup(restarted.closeSessionServices) |
| 73 | page, err := restarted.ListProjectTopics(ProjectTopicPageRequest{Scope: scope, WorkspaceRoot: root, Limit: 50}) |
| 74 | if err != nil || len(page.Items) != 0 { |
| 75 | t.Fatalf("archived topic reappeared after restart: %+v, %v", page, err) |
| 76 | } |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestNewFailedTopicCanBeArchived(t *testing.T) { |
| 82 | isolateDesktopUserDirs(t) |
| 83 | a := NewApp() |
| 84 | a.ctx = t.Context() |
| 85 | installNoopRuntimeEvents(a) |
| 86 | t.Cleanup(a.closeSessionServices) |
| 87 | topic, err := a.CreateTopic("global", "", "") |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | tab := &WorkspaceTab{ID: "failed", Scope: "global", WorkspaceRoot: globalWorkspaceRoot(), TopicID: topic.ID, StartupErr: "build failed"} |
| 92 | a.tabs[tab.ID] = tab |
| 93 | a.tabOrder = []string{tab.ID} |
| 94 | a.activeTabID = tab.ID |
| 95 | if err := a.TrashTopic(topic.ID); err != nil { |
| 96 | t.Fatalf("archive failed new topic: %v", err) |
| 97 | } |
| 98 | assertNoVisibleRuntime(t, a) |
| 99 | if !tab.removed { |
| 100 | t.Fatal("failed startup can still publish the removed topic") |
| 101 | } |
| 102 | } |
| 103 |