e2eBeforeSessionSetup.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. startCodingPlanUpgradeMockIfNeeded: async () => {
  31. events.push("coding-plan-upgrade");
  32. },
  33. startUpstreamHttpIfNeeded: async () => {
  34. events.push("upstream-http");
  35. },
  36. startPluginCdnZipFixtureIfNeeded: async () => {
  37. events.push("plugin-cdn");
  38. },
  39. startOffPeakMockIfNeeded: async () => {
  40. events.push("off-peak-mock");
  41. },
  42. }).then(() => {
  43. events.push("setup-resolved");
  44. });
  45. await waitForResetStarted(resetHome);
  46. expect(events).toEqual([
  47. "record-before-session",
  48. "configure-env",
  49. "coding-plan-team",
  50. "coding-plan-case-mode",
  51. "upstream-http",
  52. "coding-plan-upgrade",
  53. "plugin-cdn",
  54. "off-peak-mock",
  55. "reset-started",
  56. ]);
  57. expect(configureCodingPlanTeamMockForSpecs).toHaveBeenCalledWith([
  58. "./test/e2e/example.test.ts",
  59. ]);
  60. expect(resetHome).toHaveBeenCalledWith(["./test/e2e/example.test.ts"]);
  61. finishReset?.();
  62. await setupPromise;
  63. expect(events.at(-2)).toBe("reset-finished");
  64. expect(events.at(-1)).toBe("setup-resolved");
  65. });
  66. });
  67. async function waitForResetStarted(resetHome: ReturnType<typeof vi.fn>) {
  68. for (let attempt = 0; attempt < 20; attempt += 1) {
  69. if (resetHome.mock.calls.length > 0) {
  70. return;
  71. }
  72. await Promise.resolve();
  73. }
  74. throw new Error("resetHome was not called");
  75. }