e2eBeforeSessionSetup.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { describe, expect, it, vi } from "vitest";
  2. import { runE2EBeforeSessionSetup } from "./e2e/helpers/e2e-before-session.js";
  3. describe("desktop e2e beforeSession setup", () => {
  4. it("waits for async HOME reset before resolving the hook setup", async () => {
  5. const events: string[] = [];
  6. let finishReset: (() => void) | null = null;
  7. const resetHome = vi.fn(
  8. () =>
  9. new Promise<void>((resolve) => {
  10. events.push("reset-started");
  11. finishReset = () => {
  12. events.push("reset-finished");
  13. resolve();
  14. };
  15. }),
  16. );
  17. const configureCodingPlanTeamMockForSpecs = vi.fn(async () => {
  18. events.push("coding-plan-case-mode");
  19. });
  20. const setupPromise = runE2EBeforeSessionSetup({
  21. cid: "0-0",
  22. specs: ["./test/e2e/example.test.ts"],
  23. configureAgentServerEnv: () => events.push("configure-env"),
  24. recordBeforeSession: () => events.push("record-before-session"),
  25. resetHome,
  26. startCodingPlanTeamMockIfNeeded: async () => {
  27. events.push("coding-plan-team");
  28. },
  29. configureCodingPlanTeamMockForSpecs,
  30. startCodingPlanDataPlaneProxyIfNeeded: async () => {
  31. events.push("coding-plan-data-plane");
  32. },
  33. startCodingPlanUpgradeMockIfNeeded: async () => {
  34. events.push("coding-plan-upgrade");
  35. },
  36. startUpstreamHttpIfNeeded: async () => {
  37. events.push("upstream-http");
  38. },
  39. startPluginCdnZipFixtureIfNeeded: async () => {
  40. events.push("plugin-cdn");
  41. },
  42. startOffPeakMockIfNeeded: async () => {
  43. events.push("off-peak-mock");
  44. },
  45. }).then(() => {
  46. events.push("setup-resolved");
  47. });
  48. await waitForResetStarted(resetHome);
  49. expect(events).toEqual([
  50. "record-before-session",
  51. "configure-env",
  52. "coding-plan-team",
  53. "coding-plan-case-mode",
  54. "upstream-http",
  55. "coding-plan-data-plane",
  56. "coding-plan-upgrade",
  57. "plugin-cdn",
  58. "off-peak-mock",
  59. "reset-started",
  60. ]);
  61. expect(configureCodingPlanTeamMockForSpecs).toHaveBeenCalledWith([
  62. "./test/e2e/example.test.ts",
  63. ]);
  64. expect(resetHome).toHaveBeenCalledWith(["./test/e2e/example.test.ts"]);
  65. finishReset?.();
  66. await setupPromise;
  67. expect(events.at(-2)).toBe("reset-finished");
  68. expect(events.at(-1)).toBe("setup-resolved");
  69. });
  70. });
  71. async function waitForResetStarted(resetHome: ReturnType<typeof vi.fn>) {
  72. for (let attempt = 0; attempt < 20; attempt += 1) {
  73. if (resetHome.mock.calls.length > 0) {
  74. return;
  75. }
  76. await Promise.resolve();
  77. }
  78. throw new Error("resetHome was not called");
  79. }