| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/browser" |
| 10 | ) |
| 11 | |
| 12 | type recordingBrokerExecutor struct { |
| 13 | *brokerFakeExecutor |
| 14 | result json.RawMessage |
| 15 | after func() |
| 16 | } |
| 17 | |
| 18 | func (e *recordingBrokerExecutor) BrowserCapability(context.Context, string, json.RawMessage) (json.RawMessage, error) { |
| 19 | if e.after != nil { |
| 20 | e.after() |
| 21 | } |
| 22 | return e.result, nil |
| 23 | } |
| 24 | |
| 25 | func TestBrowserBrokerRecordingPublishesOnlyTransferredCompletedArtifacts(t *testing.T) { |
| 26 | for _, state := range []string{"completed", "interrupted"} { |
| 27 | t.Run(state, func(t *testing.T) { |
| 28 | exec := &recordingBrokerExecutor{brokerFakeExecutor: &brokerFakeExecutor{}, result: json.RawMessage(`{"state":"` + state + `","path":"/desktop/capture/video.webm"}`)} |
| 29 | rig := newBrokerTestRig(t, sessionResolver(exec, "/ws", map[string]bool{"/s": true})) |
| 30 | rig.conn = fakeSFTPConn{} |
| 31 | rig.relayTo = "/remote/scratch/" |
| 32 | token, _, err := rig.broker.register("host", rig.gen) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | client := browser.NewHTTPExecutor(rig.baseURL, token, nil).(browser.CapabilityExecutor) |
| 37 | call := func() (json.RawMessage, error) { |
| 38 | return client.BrowserCapability(browser.WithSession(context.Background(), "/s"), "record", json.RawMessage(`{"tabId":"tab-1","action":"status","recordingId":"r"}`)) |
| 39 | } |
| 40 | result, err := call() |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if strings.Contains(string(result), "/desktop/") { |
| 45 | t.Fatalf("desktop path leaked: %s", result) |
| 46 | } |
| 47 | if state == "completed" && (!strings.Contains(string(result), "/remote/scratch/video.webm") || len(rig.relayCalls()) != 1) { |
| 48 | t.Fatalf("artifact not relayed: %s", result) |
| 49 | } |
| 50 | if state == "interrupted" && len(rig.relayCalls()) != 0 { |
| 51 | t.Fatal("interrupted file was published") |
| 52 | } |
| 53 | exec.after = func() { rig.mu.Lock(); rig.live = false; rig.mu.Unlock() } |
| 54 | if result, err := call(); err == nil || len(result) != 0 { |
| 55 | t.Fatalf("late generation result was accepted: %s %v", result, err) |
| 56 | } |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 |