| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "reasonix/desktop/internal/workspacestate" |
| 14 | "reasonix/internal/provider" |
| 15 | "reasonix/internal/session" |
| 16 | ) |
| 17 | |
| 18 | func reopenLifecycleFixture(t *testing.T, a *App, ref session.SessionRef) (*session.Runtime, func()) { |
| 19 | t.Helper() |
| 20 | service := a.desktopSessionService("") |
| 21 | binding, err := service.Open(t.Context(), ref) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | return binding.Runtime(), func() { |
| 26 | if err := binding.Release(context.Background()); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if err := service.Close(context.Background(), ref); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func requireArchived(t *testing.T, a *App, ref session.SessionRef) { |
| 36 | t.Helper() |
| 37 | archived, err := a.ArchiveSessionTarget(SessionSelector{Ref: &ref}) |
| 38 | if err != nil || !archived.Committed { |
| 39 | t.Fatalf("ArchiveSessionTarget = %+v, %v", archived, err) |
| 40 | } |
| 41 | state, err := a.workspaceRegistry().Load(t.Context()) |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if got := state.SessionStates[ref.SessionID].Lifecycle; got != workspacestate.Archived { |
| 46 | t.Fatalf("lifecycle = %q, want archived", got) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // The session reopens with its durable history externalized, so the only |
| 51 | // record of the fixture's first message id is on disk. |
| 52 | func TestArchiveAfterACompletionReusesADurableMessageID(t *testing.T) { |
| 53 | a, ref := lifecycleFixture(t) |
| 54 | runtime, release := reopenLifecycleFixture(t, a, ref) |
| 55 | payload, err := json.Marshal(map[string]any{"message": provider.Message{ID: "user", Role: provider.RoleUser, Origin: provider.MessageOriginHost, Content: "plan approved"}}) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | _, err = runtime.Session().AppendBatch(t.Context(), "reused-id", []session.Event{{Kind: "message/complete", Payload: payload}}) |
| 60 | if !errors.Is(err, session.ErrDuplicateMessageID) { |
| 61 | t.Errorf("completion reusing a durable id: %v, want ErrDuplicateMessageID", err) |
| 62 | } |
| 63 | if _, err := runtime.Session().Flush(t.Context()); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | release() |
| 67 | requireArchived(t, a, ref) |
| 68 | } |
| 69 | |
| 70 | // Archive is the user's way out of a session that no longer opens, so a |
| 71 | // damaged store must not block it. |
| 72 | func TestArchiveSessionWhoseStoreIsDamaged(t *testing.T) { |
| 73 | a, ref := lifecycleFixture(t) |
| 74 | runtime, release := reopenLifecycleFixture(t, a, ref) |
| 75 | appendSessionTestMessage(t, runtime, "large", provider.Message{ID: "large", Role: provider.RoleAssistant, Content: strings.Repeat("x", 128<<10)}) |
| 76 | release() |
| 77 | content := filepath.Join(a.desktopSessions.root, ".content-v1") |
| 78 | if _, err := os.Stat(content); err != nil { |
| 79 | t.Fatalf("content store: %v", err) |
| 80 | } |
| 81 | if err := os.RemoveAll(content); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if _, err := a.desktopSessionService("").Query().Snapshot(t.Context(), ref); !errors.Is(err, session.ErrDamagedStore) { |
| 85 | t.Fatalf("snapshot of the damaged store: %v, want ErrDamagedStore", err) |
| 86 | } |
| 87 | requireArchived(t, a, ref) |
| 88 | } |
| 89 | |
| 90 | func TestDamagedStoreFailureCarriesItsOwnCode(t *testing.T) { |
| 91 | err := sessionOperationErrorForTarget(fmt.Errorf("%w: invalid message/complete payload at 7", session.ErrDamagedStore), "target", "op") |
| 92 | var operationErr *SessionOperationError |
| 93 | if !errors.As(err, &operationErr) || operationErr.Code != sessionOperationDamaged || operationErr.Retryable { |
| 94 | t.Fatalf("classified as %#v, want %q", err, sessionOperationDamaged) |
| 95 | } |
| 96 | if strings.Contains(operationErr.Message, "message/complete") { |
| 97 | t.Fatalf("user-visible message leaks the store detail: %q", operationErr.Message) |
| 98 | } |
| 99 | } |
| 100 |