File size: 1,360 Bytes
3e05655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { expect, test } from "bun:test"
import type { Page } from "@playwright/test"
import { measureSessionSwitch } from "../timeline/session-tab-switch-probe"

function testPage(waitFailure?: Error) {
  const stops: unknown[] = []
  const page = {
    evaluate: async (_callback: unknown, input?: unknown) => {
      if (input) return
      stops.push(undefined)
    },
    waitForFunction: async () => {
      if (waitFailure) throw waitFailure
    },
  } as unknown as Page
  return { page, stops }
}

function input(run: () => Promise<void>) {
  return {
    destinationIDs: ["destination"],
    sourceIDs: ["source"],
    lastID: "destination",
    href: "/session/destination",
    switch: run,
  }
}

test("stops sampling when the session switch fails", async () => {
  const failure = new Error("switch failed")
  const context = testPage()

  await expect(
    measureSessionSwitch(
      context.page,
      input(async () => Promise.reject(failure)),
    ),
  ).rejects.toBe(failure)

  expect(context.stops).toHaveLength(1)
})

test("stops sampling when the stable wait fails", async () => {
  const failure = new Error("stable wait failed")
  const context = testPage(failure)

  await expect(
    measureSessionSwitch(
      context.page,
      input(async () => {}),
    ),
  ).rejects.toBe(failure)

  expect(context.stops).toHaveLength(1)
})