session-tab-switch-probe.test.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { expect, test } from "bun:test"
  2. import type { Page } from "@playwright/test"
  3. import { measureSessionSwitch } from "../timeline/session-tab-switch-probe"
  4. function testPage(waitFailure?: Error) {
  5. const stops: unknown[] = []
  6. const page = {
  7. evaluate: async (_callback: unknown, input?: unknown) => {
  8. if (input) return
  9. stops.push(undefined)
  10. },
  11. waitForFunction: async () => {
  12. if (waitFailure) throw waitFailure
  13. },
  14. } as unknown as Page
  15. return { page, stops }
  16. }
  17. function input(run: () => Promise<void>) {
  18. return {
  19. destinationIDs: ["destination"],
  20. sourceIDs: ["source"],
  21. lastID: "destination",
  22. href: "/session/destination",
  23. switch: run,
  24. }
  25. }
  26. test("stops sampling when the session switch fails", async () => {
  27. const failure = new Error("switch failed")
  28. const context = testPage()
  29. await expect(
  30. measureSessionSwitch(
  31. context.page,
  32. input(async () => Promise.reject(failure)),
  33. ),
  34. ).rejects.toBe(failure)
  35. expect(context.stops).toHaveLength(1)
  36. })
  37. test("stops sampling when the stable wait fails", async () => {
  38. const failure = new Error("stable wait failed")
  39. const context = testPage(failure)
  40. await expect(
  41. measureSessionSwitch(
  42. context.page,
  43. input(async () => {}),
  44. ),
  45. ).rejects.toBe(failure)
  46. expect(context.stops).toHaveLength(1)
  47. })