| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/google/uuid" |
| 8 | ) |
| 9 | |
| 10 | func TestSessionSummaryAccountAfterRestartAndSwitch(t *testing.T) { |
| 11 | profile, workspace := t.TempDir(), t.TempDir() |
| 12 | id := uuid.NewString() |
| 13 | |
| 14 | record := &mobileRecord{ |
| 15 | mobileSession: mobileSession{ |
| 16 | mobileSummary: mobileSummary{ID: id, Workspace: workspace, Title: "对话"}, |
| 17 | Turns: []mobileTurn{}, |
| 18 | }, |
| 19 | CLIID: uuid.NewString(), Account: claudeSelection{ID: "first-account"}, |
| 20 | } |
| 21 | if err := writePrivate(mobileRecordPath(profile, id), record); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | store := &mobileCLIStore{} |
| 25 | granted := map[string]bool{workspace: true} |
| 26 | check := func(want string) { |
| 27 | t.Helper() |
| 28 | list, err := store.summaries(profile, granted, false) |
| 29 | if err != nil || len(list) != 1 { |
| 30 | t.Fatalf("summaries: %v, %v", list, err) |
| 31 | } |
| 32 | data, err := json.Marshal(list[0]) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | var view map[string]any |
| 37 | if err := json.Unmarshal(data, &view); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if view["account_id"] != want { |
| 41 | t.Fatalf("account_id = %v, want %s", view["account_id"], want) |
| 42 | } |
| 43 | } |
| 44 | check("first-account") |
| 45 | |
| 46 | record.Account.ID = "second-account" |
| 47 | store.active = map[string]*mobileRun{id: {record: record}} |
| 48 | check("second-account") |
| 49 | if err := writePrivate(mobileRecordPath(profile, id), record); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | store = &mobileCLIStore{} |
| 53 | check("second-account") |
| 54 | } |
| 55 | |
| 56 | func TestSessionSummaryRunningRequiresFreshProcessObservation(t *testing.T) { |
| 57 | profile, workspace, id := t.TempDir(), t.TempDir(), uuid.NewString() |
| 58 | record := &mobileRecord{mobileSession: mobileSession{mobileSummary: mobileSummary{ |
| 59 | ID: id, Workspace: workspace, Running: true, |
| 60 | }, Turns: []mobileTurn{}}, Terminal: true, CLIID: uuid.NewString()} |
| 61 | if err := writePrivate(mobileRecordPath(profile, id), record); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | store := &mobileCLIStore{} |
| 65 | check := func(want bool) { |
| 66 | t.Helper() |
| 67 | list, err := store.summaries(profile, map[string]bool{workspace: true}, false) |
| 68 | if err != nil || len(list) != 1 || list[0].Running != want { |
| 69 | t.Fatalf("running = %v, want %v; error = %v", list, want, err) |
| 70 | } |
| 71 | } |
| 72 | check(false) |
| 73 | store.running = map[string]bool{id: true} |
| 74 | check(true) |
| 75 | store.active = map[string]*mobileRun{id: {record: record}} |
| 76 | check(true) |
| 77 | store.running = map[string]bool{} |
| 78 | check(false) |
| 79 | } |