| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/desktop/internal/workspacestate" |
| 9 | ) |
| 10 | |
| 11 | func TestResetManualSessionOrderKeepsSavedOrderAndGroups(t *testing.T) { |
| 12 | path := filepath.Join(t.TempDir(), "workspace-state-v1.json") |
| 13 | store := workspacestate.NewStore(path) |
| 14 | ctx := t.Context() |
| 15 | id := workspacestate.GlobalWorkspaceID |
| 16 | if err := store.EnsureWorkspace(ctx, workspacestate.Workspace{ID: id}); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | for _, sessionID := range []string{"older", "newer"} { |
| 20 | if err := store.AttachSession(ctx, "", id, sessionID, ""); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | } |
| 24 | manual, _, err := store.UpdateOrganization(ctx, id, nil, func(o *workspacestate.Organization) error { |
| 25 | o.ManualOrderEnabled = true |
| 26 | o.Order = []string{workspacestate.SessionKey("older"), workspacestate.SessionKey("newer")} |
| 27 | o.Groups = []workspacestate.OrganizationGroup{{ID: "work", Title: "Work", Members: []string{workspacestate.SessionKey("older")}}} |
| 28 | return nil |
| 29 | }) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | older := ProjectNode{Key: "older", TopicID: "older", CreatedAt: 1, LastActivityAt: 1, SortOrder: 0} |
| 34 | newer := ProjectNode{Key: "newer", TopicID: "newer", CreatedAt: 2, LastActivityAt: 2, SortOrder: 1} |
| 35 | if !projectTopicLess(older, newer, "updated", manual.ManualOrderEnabled) { |
| 36 | t.Fatal("fixture did not reproduce manual order overriding recent activity") |
| 37 | } |
| 38 | if _, applied, err := store.UpdateOrganization(ctx, id, &manual.Revision, func(o *workspacestate.Organization) error { |
| 39 | return applyOrganizationMutation(o, SessionOrganizationMutation{Kind: "reset-order"}, "", "") |
| 40 | }); err != nil || !applied { |
| 41 | t.Fatalf("reset manual order: applied=%v err=%v", applied, err) |
| 42 | } |
| 43 | state, err := workspacestate.NewStore(path).Load(ctx) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | got := state.Workspaces[id].Organization |
| 48 | if got.ManualOrderEnabled { |
| 49 | t.Fatal("manual ordering remained enabled after reset") |
| 50 | } |
| 51 | if !projectTopicLess(newer, older, "updated", got.ManualOrderEnabled) || !projectTopicLess(newer, older, "created", got.ManualOrderEnabled) { |
| 52 | t.Fatal("time sorting did not resume after reset") |
| 53 | } |
| 54 | if !reflect.DeepEqual(got.Order, manual.Order) || len(got.Groups) != 1 || got.Groups[0].ID != "work" || |
| 55 | !reflect.DeepEqual(got.Groups[0].Members, manual.Groups[0].Members) { |
| 56 | t.Fatalf("reset changed saved order or groups: got=%+v before=%+v", got, manual) |
| 57 | } |
| 58 | } |
| 59 |