// Package wiretest holds the recorded contract fixtures shared by the // instance, the runner and native app builds. package wiretest import ( "amadan.net/oficina/equipo/internal/wire" "bytes" "embed" "encoding/json" "fmt" "path" "reflect" ) //go:embed testdata/frames/*.json testdata/api/*.json var fixtures embed.FS // Fixture returns one recorded document, for example "frames/reply.json" // or "api/snapshot.json". Native apps build against the same files. func Fixture(name string) []byte { raw, err := fixtures.ReadFile(path.Join("testdata", name)) if err != nil { panic(err) } return bytes.TrimSpace(raw) } // FixtureNames lists the recorded documents in one directory. func FixtureNames(dir string) []string { entries, err := fixtures.ReadDir(path.Join("testdata", dir)) if err != nil { panic(err) } out := make([]string, 0, len(entries)) for _, e := range entries { out = append(out, path.Join(dir, e.Name())) } return out } // APIType maps an api fixture to the type it records. func APIType(name string) any { switch path.Base(name) { case "me.json": return &wire.Me{} case "send-request.json", "send-request-attachments.json": return &wire.SendMessage{} case "attachment.json": return &wire.MessageAttachment{} case "send-response.json": return &wire.Sent{} case "turn.json": return &wire.Turn{} case "messages.json", "messages-attachments.json": return &wire.MessagePage{} case "cancel-accepted.json": return &wire.CancelAccepted{} case "error-conflict.json", "error-attachment.json": return &wire.Error{} case "event-thinking.json", "event-delta.json", "event-turn.json", "event-runner.json", "event-card.json", "event-project.json", "event-activity.json", "event-coordinator.json", "event-task.json", "event-step.json", "event-question.json", "event-connection.json", "event-step-activity.json", "event-preference.json": return &wire.Event{} case "thinking.json": return &wire.ThinkingWords{} case "thinking-request.json": return &wire.ThinkingChoice{} case "project.json": return &wire.Project{} case "projects.json": return &wire.ProjectList{} case "add-project-request.json": return &wire.AddProject{} case "card.json", "card-merge.json", "card-held.json", "card-publish.json": return &wire.Card{} case "answer-request.json": return &wire.AnswerCard{} case "answered.json": return &wire.Answered{} case "error-card-conflict.json": return &wire.CardConflict{} case "notebook.json": return &wire.Notebook{} case "snapshot.json": return &wire.Snapshot{} case "task.json", "task-ready.json", "task-merged.json", "task-held.json", "task-published.json": return &wire.Task{} case "tasks.json", "needs-you.json": return &wire.TaskList{} case "task-accepted.json": return &wire.TaskAccepted{} case "runners.json": return &wire.RunnerList{} case "step-log.json": return &wire.StepLog{} case "thread-request.json": return &wire.PostThread{} case "question.json": return &wire.Question{} case "ask-request.json": return &wire.AskQuestion{} case "connections.json": return &wire.ConnectionList{} case "connection-connecting-codex.json", "connection-signing-in-claude.json", "connection-metered.json", "connection-choose-model.json", "connection-choose-models.json", "connection-models.json": return &wire.Connection{} case "connect-request.json", "connect-request-api-key.json": return &wire.Connect{} case "budget.json": return &wire.SetBudget{} case "resume-request.json": return &wire.ResumeSpend{} case "model-request.json": return &wire.ChooseModel{} case "models-request.json": return &wire.SetModels{} case "preferences.json": return &wire.Preferences{} case "priority-request.json": return &wire.SetPriority{} case "run-now-request.json": return &wire.RunNow{} case "run-now-accepted.json": return &wire.RunNowAccepted{} case "connection-secret.json", "connection-code.json": return &wire.Sealed{} case "duplicate-request.json": return &wire.MarkDuplicate{} case "housekeeping.json", "housekeeping-paused.json": return &wire.Housekeeping{} case "action-combined.json", "action-undone.json", "action-refused.json": return &wire.HousekeepingAction{} case "housekeeping-actions.json": return &wire.HousekeepingActions{} case "housekeeping-request.json", "housekeeping-quiet-request.json": return &wire.SetHousekeeping{} case "detector-request.json": return &wire.SetDetector{} case "action-conflict.json": return &wire.ActionConflict{} case "event-housekeeping.json", "event-housekeeping-summary.json": return &wire.Event{} case "card-housekeeping.json", "card-digest.json", "card-advice.json": return &wire.Card{} case "advice.json": return &wire.AdviceList{} case "advice-action.json": return &wire.AdviceAction{} case "usage-month.json": return &wire.UsageTotals{} } return nil } // SameShape reports whether a and b have the same JSON object keys at every // level. Values may differ: ids, times and text are not part of the shape. func SameShape(a, b []byte) error { var x, y any if err := json.Unmarshal(a, &x); err != nil { return err } if err := json.Unmarshal(b, &y); err != nil { return err } return shape("$", x, y) } func shape(at string, a, b any) error { switch x := a.(type) { case map[string]any: y, ok := b.(map[string]any) if !ok { return fmt.Errorf("%s: object vs %T", at, b) } for k := range x { if _, ok := y[k]; !ok { return fmt.Errorf("%s.%s: missing", at, k) } } for k := range y { if _, ok := x[k]; !ok { return fmt.Errorf("%s.%s: unexpected", at, k) } if err := shape(at+"."+k, x[k], y[k]); err != nil { return err } } case []any: y, ok := b.([]any) if !ok { return fmt.Errorf("%s: array vs %T", at, b) } if len(x) > 0 && len(y) > 0 { return shape(at+"[0]", x[0], y[0]) } default: if a != nil && b != nil && reflect.TypeOf(a) != reflect.TypeOf(b) { return fmt.Errorf("%s: %T vs %T", at, a, b) } } return nil }