package opencode import ( "context" "path/filepath" "strings" "testing" ) // exportFixture writes a fake `opencode` CLI that answers only the session // export invocation with the given JSON body, accepting both the v1 top-level // `export ` and the v2 `session export ` forms, so the native export // path is exercised without a real service. func exportFixture(t *testing.T, id, body string) string { t.Helper() script := "#!/bin/sh\n" + "[ \"$1\" = export ] && shift || { [ \"$1\" = session ] && [ \"$2\" = export ] && shift 2 || exit 8; }\n" + "[ \"$1\" = " + id + " ] || exit 9\n" + "cat <<'EOF'\n" + body + "\nEOF\n" return writeFixture(t, script) } func TestExportSessionDecodesRolesPartsAndUnknownTypes(t *testing.T) { id := "ses_ok" body := `{"info":{"id":"ses_ok"},"messages":[` + `{"info":{"id":"m1","role":"assistant","modelID":"gpt"},"parts":[` + `{"type":"text","text":"hello"},` + `{"type":"reasoning","text":"because"},` + `{"type":"tool","name":"read","state":{"status":"completed","input":{"path":"a.go"},"content":[{"type":"text","text":"ok"}]}},` + `{"type":"file","uri":"file:///tmp/x"}]},` + `{"info":{"id":"m2","role":"user"},"text":"hi"}]}` bin := exportFixture(t, id, body) session, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", id) if err != nil { t.Fatal(err) } if session.ID != id || session.Backend != "v2" || session.Truncated { t.Fatalf("unexpected session: %+v", session) } if len(session.Messages) != 2 { t.Fatalf("messages = %d, want 2", len(session.Messages)) } assistant := session.Messages[0] if assistant.Role != "assistant" || assistant.ID != "m1" || assistant.Model != "gpt" { t.Fatalf("assistant metadata wrong: %+v", assistant) } if len(assistant.Parts) != 4 { t.Fatalf("assistant parts = %d, want 4 (unknown type kept)", len(assistant.Parts)) } if assistant.Parts[0].Type != "text" || assistant.Parts[0].Text != "hello" { t.Fatalf("text part wrong: %+v", assistant.Parts[0]) } if assistant.Parts[1].Type != "reasoning" || assistant.Parts[1].Text != "because" { t.Fatalf("reasoning part wrong: %+v", assistant.Parts[1]) } tool := assistant.Parts[2] if tool.Type != "tool" || tool.Name != "read" || tool.State == nil || tool.State.Status != "completed" { t.Fatalf("tool part wrong: %+v", tool) } if len(tool.State.Input) == 0 || string(tool.State.Input) != `{"path":"a.go"}` { t.Fatalf("tool input lost: %s", tool.State.Input) } if len(tool.State.Content) != 1 || tool.State.Content[0].Text != "ok" { t.Fatalf("tool output lost: %+v", tool.State.Content) } unknown := assistant.Parts[3] if unknown.Type != "file" || !strings.Contains(string(unknown.Raw), "file:///tmp/x") { t.Fatalf("unknown part raw JSON lost: %+v", unknown) } user := session.Messages[1] if user.Role != "user" || len(user.Parts) != 1 || user.Parts[0].Text != "hi" { t.Fatalf("user message-level text not surfaced: %+v", user) } } func TestExportSessionRejectsMismatchedOrInvalidID(t *testing.T) { bin := exportFixture(t, "ses_other", `{"info":{"id":"ses_other"},"messages":[{"info":{"role":"assistant"}}]}`) if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", "ses_want"); err == nil { t.Fatal("mismatched session export accepted") } if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", "not-a-session"); err == nil { t.Fatal("invalid session id accepted without running the CLI") } } func TestExportSessionFailsSanitizedWhenCLIUnavailable(t *testing.T) { bin := filepath.Join(t.TempDir(), "absent-opencode") if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", "ses_ok"); err == nil { t.Fatal("missing CLI must fail the export") } } func TestExportNativeReportsBoundedOverflow(t *testing.T) { id := "ses_ok" body := `{"info":{"id":"ses_ok"},"messages":[{"info":{"role":"assistant"},"parts":[{"type":"text","text":"` + strings.Repeat("x", 256) + `"}]}]}` bin := exportFixture(t, id, body) res, err := exportNative(context.Background(), bin, t.TempDir(), "v2", id, 32) if err != nil { t.Fatal(err) } if !res.truncated { t.Fatal("bounded stdout overflow was not reported") } if len(res.envelope) != 32 { t.Fatalf("bounded prefix = %d bytes, want 32", len(res.envelope)) } } func TestExportNativeUsesBackendSubcommand(t *testing.T) { id := "ses_ok" body := `{"info":{"id":"ses_ok"},"messages":[{"info":{"role":"assistant"},"parts":[{"type":"text","text":"hello"}]}]}` bin := exportFixture(t, id, body) // Both a v1 (top-level export) and a v2 (session export) CLI accept the one // shared invocation. for _, backend := range []string{"v1", "v2"} { session, err := ExportSession(context.Background(), bin, t.TempDir(), backend, id) if err != nil || len(session.Messages) != 1 { t.Fatalf("backend %s export failed: %v %+v", backend, err, session) } } } func TestExportSessionKeepsCompleteTurnsFromOverCapSource(t *testing.T) { id := "ses_ok" huge := strings.Repeat("x", MaxSessionExport+(1<<20)) body := `{"info":{"id":"ses_ok"},"messages":[` + `{"info":{"role":"assistant"},"parts":[{"type":"text","text":"first"}]},` + `{"info":{"role":"user"},"parts":[{"type":"text","text":"` + huge + `"}]}]}` bin := exportFixture(t, id, body) session, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", id) if err != nil { t.Fatal(err) } if !session.Truncated { t.Fatal("an over-cap source must be reported truncated") } if len(session.Messages) != 1 || session.Messages[0].Parts[0].Text != "first" { t.Fatalf("complete turn before the cut was lost: %+v", session.Messages) } } func TestDecodeExportPrefixRejectsMismatchedID(t *testing.T) { id := "ses_ok" full := `{"info":{"id":"ses_ok"},"messages":[` + `{"info":{"role":"assistant"},"parts":[{"type":"text","text":"first"}]},` + `{"info":{"role":"user"},"parts":[{"type":"text","text":"second"}]}]}` // Cut mid-way through the second message so only the first is complete. cut := strings.Index(full, `"second"`) + len(`"second"`) - 3 records, truncated, err := decodeExportPrefix([]byte(full[:cut]), id) if err != nil { t.Fatal(err) } if !truncated || len(records) != 1 || records[0].role() != "assistant" { t.Fatalf("prefix decode = %d records, truncated=%v", len(records), truncated) } if _, _, err := decodeExportPrefix([]byte(full), "ses_other"); err == nil { t.Fatal("mismatched prefix session accepted") } }