-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest.ts
More file actions
196 lines (169 loc) · 5.81 KB
/
test.ts
File metadata and controls
196 lines (169 loc) · 5.81 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {
expect,
test as base,
type Browser,
type BrowserContext,
type Page,
} from "@playwright/test";
import * as fs from "node:fs";
import { MEETINGS_STATE_FILE, type MeetingsState } from "../global-setup";
import { STUB_MEDIA_SCRIPT } from "./media";
import { loginViaApi } from "../helpers/auth";
import { clearMeetingCreateRateLimit, createMeetingViaApi, type MeetingType } from "../helpers/meeting";
const isCI = !!process.env.CI;
const previewTimeout = isCI ? 45_000 : 20_000;
const meetingReadyTimeout = isCI ? 60_000 : 20_000;
function readMeetingsState(): MeetingsState {
const raw = fs.readFileSync(MEETINGS_STATE_FILE, "utf-8");
return JSON.parse(raw) as MeetingsState;
}
interface Participant {
context: BrowserContext;
page: Page;
joinMeeting(meetingId: string): Promise<void>;
joinAsGuest(meetingId: string, guestName: string): Promise<void>;
joinAsHost(meetingId: string): Promise<void>;
endCall(): Promise<void>;
}
interface TestFixtures {
hostPage: Page;
meetingId: string;
restrictedMeetingId: string;
createMeeting: (meetingType?: MeetingType) => Promise<string>;
createMeetingViaUi: (meetingType?: MeetingType) => Promise<string>;
createParticipant: () => Promise<Participant>;
}
async function prepareContext(context: BrowserContext): Promise<void> {
await context.addInitScript({ content: STUB_MEDIA_SCRIPT });
await context.grantPermissions(["camera", "microphone"]);
}
async function waitForMeetingReady(page: Page): Promise<void> {
await page.getByTestId("meeting-layout").waitFor({
state: "visible",
timeout: meetingReadyTimeout,
});
await expect(page.getByTestId("meeting-toolbar")).toBeVisible();
await expect(page.getByTestId("toolbar-end-call")).toBeVisible();
}
async function joinFromPreview(page: Page): Promise<void> {
const preview = page.getByTestId("meeting-preview");
const meetingLayout = page.getByTestId("meeting-layout");
const joinButton = page.getByTestId("join-meeting-preview-button");
await Promise.race([
preview.waitFor({ state: "visible", timeout: previewTimeout }),
meetingLayout.waitFor({ state: "visible", timeout: previewTimeout }),
]);
if (
!(await meetingLayout.isVisible().catch(() => false)) &&
(await preview.isVisible().catch(() => false))
) {
await joinButton.waitFor({ state: "visible", timeout: previewTimeout });
await expect(joinButton).toBeEnabled({ timeout: previewTimeout });
try {
await joinButton.click({ timeout: previewTimeout });
} catch (error) {
const previewStillVisible = await preview.isVisible().catch(() => false);
const layoutVisible = await meetingLayout.isVisible().catch(() => false);
if (previewStillVisible && !layoutVisible) {
throw error;
}
}
}
await waitForMeetingReady(page);
}
async function createMeetingViaUi(
page: Page,
meetingType: MeetingType = "open",
): Promise<string> {
await page.getByTestId("home-page").waitFor({ state: "visible", timeout: 20_000 });
if (meetingType === "open") {
await page.getByTestId("create-open-meeting-button").click();
} else {
await page.getByTestId("create-meeting-options").click();
await page.getByRole("menuitem", { name: "Create a restricted meeting" }).click();
}
await page.waitForURL(/\/meet\/[a-z0-9-]+(?:\?created=true)?$/);
const url = new URL(page.url());
const match = url.pathname.match(/\/meet\/([a-z0-9-]+)$/);
if (!match) {
throw new Error(`Could not extract meeting id from URL: ${page.url()}`);
}
return match[1];
}
async function buildParticipant(browser: Browser): Promise<Participant> {
const context = await browser.newContext();
await prepareContext(context);
const page = await context.newPage();
return {
context,
page,
async joinMeeting(meetingId: string) {
await page.goto(`/meet/${meetingId}`);
},
async joinAsGuest(meetingId: string, guestName: string) {
await page.goto(`/meet/${meetingId}`);
await expect(page.getByTestId("meeting-preview")).toBeVisible({
timeout: previewTimeout,
});
const guestNameInput = page.getByPlaceholder("John Doe");
await guestNameInput.fill(guestName);
await expect(guestNameInput).toHaveValue(guestName);
await expect(page.getByTestId("join-meeting-preview-button")).toBeEnabled({
timeout: previewTimeout,
});
await joinFromPreview(page);
},
async joinAsHost(meetingId: string) {
await loginViaApi(context.request);
await page.goto("/meet/");
await page.goto(`/meet/${meetingId}?created=true`);
await joinFromPreview(page);
},
async endCall() {
await page.getByTestId("toolbar-end-call").click();
await page.waitForURL("**/meet/");
},
};
}
export const test = base.extend<TestFixtures>({
hostPage: async ({ browser }, use) => {
const context = await browser.newContext();
await prepareContext(context);
await loginViaApi(context.request);
const page = await context.newPage();
await page.goto("/meet/");
await use(page);
await context.close();
},
createMeeting: async ({ hostPage }, use) => {
await use(async (meetingType = "open") => {
return createMeetingViaApi(hostPage.request, meetingType);
});
},
createMeetingViaUi: async ({ hostPage }, use) => {
await use(async (meetingType = "open") => {
return createMeetingViaUi(hostPage, meetingType);
});
},
meetingId: async ({}, use) => {
await use(readMeetingsState().openMeetingId);
},
restrictedMeetingId: async ({}, use) => {
await use(readMeetingsState().restrictedMeetingId);
},
createParticipant: async ({ browser }, use) => {
const participants: Participant[] = [];
await use(async () => {
const participant = await buildParticipant(browser);
participants.push(participant);
return participant;
});
await Promise.all(
participants.map((participant) => participant.context.close()),
);
},
});
test.beforeEach(async ({ hostPage }) => {
await clearMeetingCreateRateLimit(hostPage.request);
});
export { expect, joinFromPreview };