package thintest import ( "context" "encoding/json" "errors" "net/http" "strings" "time" "amadan.net/oficina/equipo/internal/wire" "github.com/coder/websocket" ) // FakeRunner is a scripted runner stream client for instance contract // tests. It records every frame in both directions. type FakeRunner struct { Conn *websocket.Conn Welcome wire.Welcome Received [][]byte Sent [][]byte } // DialRunner connects and says hello with generation (protocol 1, the // slice 4 hello). func DialRunner(ctx context.Context, origin, token string, generation int64) (*FakeRunner, error) { return DialRunnerHello(ctx, origin, token, wire.Hello{Generation: generation, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}}) } // WorkerHello is a protocol 2 hello from a Mac worker, with the fixture's // capability report. func WorkerHello(generation int64) wire.Hello { return wire.Hello{Generation: generation, Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}, Protocol: wire.Protocol2, Platform: &wire.Platform{OS: "darwin", Arch: "arm64"}, Label: "Tito Studio", Sandbox: wire.SandboxSeatbelt, Capabilities: &wire.Capabilities{Xcode: wire.Xcode{Version: "16.2", Build: "16C5032a"}, Simulators: []wire.Simulator{{Name: "iPhone 16 Pro", Runtime: "iOS 18.2", UDID: "5C0A1E2B-3D4F-4A5B-9C6D-7E8F90A1B2C3", State: "Shutdown"}}, Tags: []string{wire.TagMacOS, wire.TagXcode, wire.TagIOSSimulator}, Probes: []wire.Probe{{ID: "P1", Verdict: wire.ProbePassed, Detail: "xcode:16.2,16C5032a;ios_simulators:1;computer:Tito Studio"}, {ID: "P2", Verdict: wire.ProbeFailed, Detail: "allowed:test.keychain;srt_ran_xcode"}, {ID: "P3", Verdict: wire.ProbeSkipped}, {ID: "P4", Verdict: wire.ProbePassed}, {ID: "P5", Verdict: wire.ProbeSkipped}, {ID: "P6", Verdict: wire.ProbePassed}, {ID: "P7", Verdict: wire.ProbePassed}}}} } // DialRunnerHello connects and says hello. func DialRunnerHello(ctx context.Context, origin, token string, hello wire.Hello) (*FakeRunner, error) { return DialRunnerFrom(ctx, origin, token, hello, nil) } // DialRunnerFrom is DialRunnerHello with extra request headers (an edge's // X-Forwarded-For). func DialRunnerFrom(ctx context.Context, origin, token string, hello wire.Hello, extra http.Header) (*FakeRunner, error) { url := "ws" + strings.TrimPrefix(origin, "http") + wire.StreamPath header := http.Header{"Authorization": {"Bearer " + token}} for k, v := range extra { header[k] = v } c, _, err := websocket.Dial(ctx, url, &websocket.DialOptions{HTTPHeader: header}) if err != nil { return nil, err } c.SetReadLimit(wire.MaxFrameBytes) r := &FakeRunner{Conn: c} if err = r.Send(ctx, "h0", wire.FrameHello, hello); err != nil { return nil, err } f, body, err := r.Next(ctx) if err != nil { return nil, err } w, ok := body.(*wire.Welcome) if !ok || f.Type != wire.FrameWelcome { return nil, errors.New("no welcome") } r.Welcome = *w return r, nil } func (r *FakeRunner) Send(ctx context.Context, id, kind string, body any) error { f, err := wire.NewFrame(id, kind, body) if err != nil { return err } raw, _ := json.Marshal(f) r.Sent = append(r.Sent, raw) return r.Conn.Write(ctx, websocket.MessageText, raw) } // frameWait bounds the wait for one frame. A loaded CI runner (load 40 // and up) has taken more than 10 seconds to send the next frame of a // transfer contract; a wait that ends closes the stream, so it fails the // test rather than a slow box being the verdict. const frameWait = 45 * time.Second // Next reads one frame. func (r *FakeRunner) Next(ctx context.Context) (wire.Frame, any, error) { ctx, cancel := context.WithTimeout(ctx, frameWait) defer cancel() _, raw, err := r.Conn.Read(ctx) if err != nil { return wire.Frame{}, nil, err } r.Received = append(r.Received, raw) return wire.ParseFrame(raw) } // Expect reads frames until one of kind arrives, skipping acks unless // kind is ack. func (r *FakeRunner) Expect(ctx context.Context, kind string) (wire.Frame, any, error) { for { f, body, err := r.Next(ctx) if err != nil || f.Type == kind { return f, body, err } if f.Type != wire.FrameAck { return f, body, errors.New("unexpected frame " + f.Type) } } } func (r *FakeRunner) Close() { r.Conn.CloseNow() } // CloseStatus is the close code the instance sent, if any. func CloseStatus(err error) websocket.StatusCode { return websocket.CloseStatus(err) }