oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// Package wiretest holds the recorded contract fixtures shared by the
2// instance, the runner and native app builds.
3package wiretest
4
5import (
6 "amadan.net/oficina/equipo/internal/wire"
7 "bytes"
8 "embed"
9 "encoding/json"
10 "fmt"
11 "path"
12 "reflect"
13)
14
15//go:embed testdata/frames/*.json testdata/api/*.json
16var fixtures embed.FS
17
18// Fixture returns one recorded document, for example "frames/reply.json"
19// or "api/snapshot.json". Native apps build against the same files.
20func Fixture(name string) []byte {
21 raw, err := fixtures.ReadFile(path.Join("testdata", name))
22 if err != nil {
23 panic(err)
24 }
25 return bytes.TrimSpace(raw)
26}
27
28// FixtureNames lists the recorded documents in one directory.
29func FixtureNames(dir string) []string {
30 entries, err := fixtures.ReadDir(path.Join("testdata", dir))
31 if err != nil {
32 panic(err)
33 }
34 out := make([]string, 0, len(entries))
35 for _, e := range entries {
36 out = append(out, path.Join(dir, e.Name()))
37 }
38 return out
39}
40
41// APIType maps an api fixture to the type it records.
42func APIType(name string) any {
43 switch path.Base(name) {
44 case "me.json":
45 return &wire.Me{}
46 case "send-request.json", "send-request-attachments.json":
47 return &wire.SendMessage{}
48 case "attachment.json":
49 return &wire.MessageAttachment{}
50 case "send-response.json":
51 return &wire.Sent{}
52 case "turn.json":
53 return &wire.Turn{}
54 case "messages.json", "messages-attachments.json":
55 return &wire.MessagePage{}
56 case "cancel-accepted.json":
57 return &wire.CancelAccepted{}
58 case "error-conflict.json", "error-attachment.json":
59 return &wire.Error{}
60 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":
61 return &wire.Event{}
62 case "thinking.json":
63 return &wire.ThinkingWords{}
64 case "thinking-request.json":
65 return &wire.ThinkingChoice{}
66 case "project.json":
67 return &wire.Project{}
68 case "projects.json":
69 return &wire.ProjectList{}
70 case "add-project-request.json":
71 return &wire.AddProject{}
72 case "card.json", "card-merge.json", "card-held.json", "card-publish.json":
73 return &wire.Card{}
74 case "answer-request.json":
75 return &wire.AnswerCard{}
76 case "answered.json":
77 return &wire.Answered{}
78 case "error-card-conflict.json":
79 return &wire.CardConflict{}
80 case "notebook.json":
81 return &wire.Notebook{}
82 case "snapshot.json":
83 return &wire.Snapshot{}
84 case "task.json", "task-ready.json", "task-merged.json", "task-held.json", "task-published.json":
85 return &wire.Task{}
86 case "tasks.json", "needs-you.json":
87 return &wire.TaskList{}
88 case "task-accepted.json":
89 return &wire.TaskAccepted{}
90 case "runners.json":
91 return &wire.RunnerList{}
92 case "step-log.json":
93 return &wire.StepLog{}
94 case "thread-request.json":
95 return &wire.PostThread{}
96 case "question.json":
97 return &wire.Question{}
98 case "ask-request.json":
99 return &wire.AskQuestion{}
100 case "connections.json":
101 return &wire.ConnectionList{}
102 case "connection-connecting-codex.json", "connection-signing-in-claude.json", "connection-metered.json", "connection-choose-model.json", "connection-choose-models.json", "connection-models.json":
103 return &wire.Connection{}
104 case "connect-request.json", "connect-request-api-key.json":
105 return &wire.Connect{}
106 case "budget.json":
107 return &wire.SetBudget{}
108 case "resume-request.json":
109 return &wire.ResumeSpend{}
110 case "model-request.json":
111 return &wire.ChooseModel{}
112 case "models-request.json":
113 return &wire.SetModels{}
114 case "preferences.json":
115 return &wire.Preferences{}
116 case "priority-request.json":
117 return &wire.SetPriority{}
118 case "run-now-request.json":
119 return &wire.RunNow{}
120 case "run-now-accepted.json":
121 return &wire.RunNowAccepted{}
122 case "connection-secret.json", "connection-code.json":
123 return &wire.Sealed{}
124 case "duplicate-request.json":
125 return &wire.MarkDuplicate{}
126 case "housekeeping.json", "housekeeping-paused.json":
127 return &wire.Housekeeping{}
128 case "action-combined.json", "action-undone.json", "action-refused.json":
129 return &wire.HousekeepingAction{}
130 case "housekeeping-actions.json":
131 return &wire.HousekeepingActions{}
132 case "housekeeping-request.json", "housekeeping-quiet-request.json":
133 return &wire.SetHousekeeping{}
134 case "detector-request.json":
135 return &wire.SetDetector{}
136 case "action-conflict.json":
137 return &wire.ActionConflict{}
138 case "event-housekeeping.json", "event-housekeeping-summary.json":
139 return &wire.Event{}
140 case "card-housekeeping.json", "card-digest.json", "card-advice.json":
141 return &wire.Card{}
142 case "advice.json":
143 return &wire.AdviceList{}
144 case "advice-action.json":
145 return &wire.AdviceAction{}
146 case "usage-month.json":
147 return &wire.UsageTotals{}
148 }
149 return nil
150}
151
152// SameShape reports whether a and b have the same JSON object keys at every
153// level. Values may differ: ids, times and text are not part of the shape.
154func SameShape(a, b []byte) error {
155 var x, y any
156 if err := json.Unmarshal(a, &x); err != nil {
157 return err
158 }
159 if err := json.Unmarshal(b, &y); err != nil {
160 return err
161 }
162 return shape("$", x, y)
163}
164
165func shape(at string, a, b any) error {
166 switch x := a.(type) {
167 case map[string]any:
168 y, ok := b.(map[string]any)
169 if !ok {
170 return fmt.Errorf("%s: object vs %T", at, b)
171 }
172 for k := range x {
173 if _, ok := y[k]; !ok {
174 return fmt.Errorf("%s.%s: missing", at, k)
175 }
176 }
177 for k := range y {
178 if _, ok := x[k]; !ok {
179 return fmt.Errorf("%s.%s: unexpected", at, k)
180 }
181 if err := shape(at+"."+k, x[k], y[k]); err != nil {
182 return err
183 }
184 }
185 case []any:
186 y, ok := b.([]any)
187 if !ok {
188 return fmt.Errorf("%s: array vs %T", at, b)
189 }
190 if len(x) > 0 && len(y) > 0 {
191 return shape(at+"[0]", x[0], y[0])
192 }
193 default:
194 if a != nil && b != nil && reflect.TypeOf(a) != reflect.TypeOf(b) {
195 return fmt.Errorf("%s: %T vs %T", at, a, b)
196 }
197 }
198 return nil
199}
200