| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "reasonix/internal/provider" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestProviderCacheSessionLifecycle(t *testing.T) { |
| 12 | makeAgent := func() *Agent { a := &Agent{}; a.sess.conversation = NewSession(""); return a } |
| 13 | id := func(a *Agent, ctx context.Context) string { |
| 14 | req, _ := http.NewRequestWithContext(a.withProviderCacheSession(ctx), http.MethodPost, "https://opencode.ai/zen/go/v1/responses", nil) |
| 15 | provider.ApplyOpenCodeGoHeaders(req, "https://opencode.ai/zen/go/v1", provider.NewClientIdentityHeaders()) |
| 16 | return req.Header.Get("x-opencode-session") |
| 17 | } |
| 18 | a := makeAgent() |
| 19 | ctx := context.Background() |
| 20 | first := id(a, ctx) |
| 21 | if first != id(a, ctx) { |
| 22 | t.Fatal("ephemeral conversation changed between turns") |
| 23 | } |
| 24 | if id(makeAgent(), a.withProviderCacheSession(ctx)) == first { |
| 25 | t.Fatal("child reused parent session") |
| 26 | } |
| 27 | a.SetSessionPath("/private/root/conversation.jsonl") |
| 28 | bound := id(a, ctx) |
| 29 | b := makeAgent() |
| 30 | b.SetSessionPath(a.SessionPath()) |
| 31 | if bound != id(b, ctx) || strings.Contains(bound, "private") { |
| 32 | t.Fatal("resume identity changed or exposed local path") |
| 33 | } |
| 34 | b.SetSessionPath("/private/root/new-session.jsonl") |
| 35 | if bound == id(b, ctx) { |
| 36 | t.Fatal("new session reused old identity") |
| 37 | } |
| 38 | } |
| 39 |