| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net/http" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/equipo/internal/wire" |
| 18 | ) |
| 19 | |
| 20 | // The native apps (native/apple, EquipoKit) read the person API with a |
| 21 | // device bearer, as this suite does. Their test fixtures are this |
| 22 | // instance's own answers, captured from the rollup rig with |
| 23 | // EQUIPO_UPDATE_NATIVE_FIXTURES=1. Every run checks that each checked-in |
| 24 | // fixture still decodes strictly into its wire type: a field the server |
| 25 | // no longer sends, or one it never sent, fails here. |
| 26 | const nativeFixtureDir = "../../native/apple/Tests/EquipoKitTests/Fixtures/thin" |
| 27 | |
| 28 | var nativeFixtureTypes = map[string]func() any{ |
| 29 | "snapshot": func() any { return &wire.Snapshot{} }, |
| 30 | "tasks": func() any { return &wire.TaskList{} }, |
| 31 | "needs-you": func() any { return &wire.TaskList{} }, |
| 32 | "projects": func() any { return &wire.ProjectList{} }, |
| 33 | "task": func() any { return &wire.Task{} }, |
| 34 | "messages": func() any { return &wire.MessagePage{} }, |
| 35 | "sent": func() any { return &wire.Sent{} }, |
| 36 | "answered": func() any { return &wire.Answered{} }, |
| 37 | "card-conflict": func() any { return &wire.CardConflict{} }, |
| 38 | "thread-entry": func() any { return &wire.ThreadEntry{} }, |
| 39 | "events": func() any { return &[]wire.Event{} }, |
| 40 | } |
| 41 | |
| 42 | func TestNativeFixtures(t *testing.T) { |
| 43 | if os.Getenv("EQUIPO_UPDATE_NATIVE_FIXTURES") == "1" { |
| 44 | captureNativeFixtures(t) |
| 45 | } |
| 46 | for name, typ := range nativeFixtureTypes { |
| 47 | raw, err := os.ReadFile(filepath.Join(nativeFixtureDir, name+".json")) |
| 48 | if err != nil { |
| 49 | t.Fatalf("%s: %v (capture with EQUIPO_UPDATE_NATIVE_FIXTURES=1)", name, err) |
| 50 | } |
| 51 | d := json.NewDecoder(bytes.NewReader(raw)) |
| 52 | d.DisallowUnknownFields() |
| 53 | if err := d.Decode(typ()); err != nil { |
| 54 | t.Errorf("%s.json no longer matches its wire type: %v", name, err) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func captureNativeFixtures(t *testing.T) { |
| 60 | r := newRollupRig(t) |
| 61 | ctx := r.ctx |
| 62 | get := func(path string) []byte { return nativeRaw(t, ctx, r.client, "GET", path, nil, 200) } |
| 63 | write := func(name string, raw []byte) { |
| 64 | var v any |
| 65 | if err := json.Unmarshal(raw, &v); err != nil { |
| 66 | t.Fatalf("%s: %v", name, err) |
| 67 | } |
| 68 | pretty, _ := json.MarshalIndent(v, "", " ") |
| 69 | if err := os.MkdirAll(nativeFixtureDir, 0o755); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if err := os.WriteFile(filepath.Join(nativeFixtureDir, name+".json"), append(pretty, '\n'), 0o644); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | } |
| 76 | write("snapshot", get("/api/v1/snapshot")) |
| 77 | write("tasks", get("/api/v1/tasks")) |
| 78 | write("needs-you", get("/api/v1/needs-you")) |
| 79 | write("projects", get("/api/v1/projects")) |
| 80 | write("task", get("/api/v1/tasks/"+r.asked)) |
| 81 | write("messages", get("/api/v1/messages?limit=20")) |
| 82 | write("thread-entry", nativeRaw(t, ctx, r.client, "POST", "/api/v1/tasks/"+r.asked+"/thread", |
| 83 | wire.PostThread{Text: "Does this cover the iPad too?", IdempotencyKey: "native-fixture-thread"}, 0)) |
| 84 | write("sent", nativeRaw(t, ctx, r.client, "POST", "/api/v1/messages", |
| 85 | wire.SendMessage{Text: "What is left?", IdempotencyKey: "native-fixture-send"}, 0)) |
| 86 | // Answer the open merge card with its last choice, then again with the |
| 87 | // revision it had: the second is the conflict a stale native view gets. |
| 88 | card := r.askCard |
| 89 | choice := card.Options[len(card.Options)-1] |
| 90 | write("answered", nativeRaw(t, ctx, r.client, "POST", "/api/v1/cards/"+card.ID+"/answer", |
| 91 | wire.AnswerCard{Choice: choice, Revision: card.Revision}, 200)) |
| 92 | write("card-conflict", nativeRaw(t, ctx, r.client, "POST", "/api/v1/cards/"+card.ID+"/answer", |
| 93 | wire.AnswerCard{Choice: choice, Revision: card.Revision}, 409)) |
| 94 | write("events", nativeEvents(t, ctx, r.client)) |
| 95 | } |
| 96 | |
| 97 | // nativeRaw sends one request with the device bearer and returns the body. |
| 98 | // want 0 accepts any 2xx. |
| 99 | func nativeRaw(t *testing.T, ctx context.Context, c *Client, method, path string, body any, want int) []byte { |
| 100 | t.Helper() |
| 101 | var reader io.Reader |
| 102 | if body != nil { |
| 103 | raw, _ := json.Marshal(body) |
| 104 | reader = bytes.NewReader(raw) |
| 105 | } |
| 106 | req, err := http.NewRequestWithContext(ctx, method, c.Origin+path, reader) |
| 107 | if err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | req.Header.Set("Authorization", "Bearer "+c.Token) |
| 111 | if body != nil { |
| 112 | req.Header.Set("Content-Type", "application/json") |
| 113 | req.Header.Set("Idempotency-Key", fmt.Sprintf("native-fixture-%d", time.Now().UnixNano())) |
| 114 | } |
| 115 | res, err := c.HTTP.Do(req) |
| 116 | if err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | defer res.Body.Close() |
| 120 | raw, _ := io.ReadAll(io.LimitReader(res.Body, 4<<20)) |
| 121 | if (want != 0 && res.StatusCode != want) || (want == 0 && res.StatusCode/100 != 2) { |
| 122 | t.Fatalf("%s %s = %d: %s", method, path, res.StatusCode, raw) |
| 123 | } |
| 124 | return raw |
| 125 | } |
| 126 | |
| 127 | // nativeEvents reads the conversation's event stream from the start for a |
| 128 | // moment and returns the events it carried, as a JSON array. |
| 129 | func nativeEvents(t *testing.T, ctx context.Context, c *Client) []byte { |
| 130 | t.Helper() |
| 131 | ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
| 132 | defer cancel() |
| 133 | req, _ := http.NewRequestWithContext(ctx, "GET", c.Origin+"/api/v1/events?after=0", nil) |
| 134 | req.Header.Set("Authorization", "Bearer "+c.Token) |
| 135 | res, err := c.HTTP.Do(req) |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | defer res.Body.Close() |
| 140 | var events []json.RawMessage |
| 141 | s := bufio.NewScanner(res.Body) |
| 142 | s.Buffer(make([]byte, 1<<20), 4<<20) |
| 143 | for s.Scan() && len(events) < 12 { |
| 144 | if line := s.Text(); strings.HasPrefix(line, "data: ") { |
| 145 | events = append(events, json.RawMessage(strings.TrimPrefix(line, "data: "))) |
| 146 | } |
| 147 | } |
| 148 | if len(events) == 0 { |
| 149 | t.Fatal("the event stream carried nothing") |
| 150 | } |
| 151 | raw, _ := json.Marshal(events) |
| 152 | return raw |
| 153 | } |
| 154 | |