forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail-session-smoke.mjs
More file actions
207 lines (192 loc) · 8.25 KB
/
Copy pathgmail-session-smoke.mjs
File metadata and controls
207 lines (192 loc) · 8.25 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
197
198
199
200
201
202
203
204
205
206
207
// Manual smoke for the Gmail "session" connector (Option B).
//
// Launches the REAL built app (out/main/index.js) via Playwright's _electron and
// exercises the real IPC: window.omi.gmailSessionConnect() opens the Omi-owned login
// BrowserWindow on the persist:omi-gmail partition. We read the login window's URL,
// title, and a snippet of its text straight from the main process, screenshot it, and
// assert it reached accounts.google.com — proving the window/partition wiring WITHOUT
// completing (or needing) a Google login. Requires network to reach Google.
//
// Two modes:
// node scripts/gmail-session-smoke.mjs (default) open → assert URL → shot → close
// node scripts/gmail-session-smoke.mjs --login open and WAIT up to 5 min for you to
// sign in, then fetch recent mail and
// print the count (Chris's one-time smoke)
//
// Build first if needed: `npx electron-vite build`.
import { _electron as electron } from 'playwright'
import { fileURLToPath } from 'node:url'
import { mkdtempSync, mkdirSync, writeFileSync, existsSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
const mainEntry = path.join(root, 'out', 'main', 'index.js')
const shotsDir = path.join(root, '.playwright-mcp', 'gmail-session')
const LOGIN_MODE = process.argv.includes('--login')
// A clearly-fake email to prove the login_hint arg reaches Google's URL (renderer->main).
const SMOKE_LOGIN_HINT = 'omi.smoke.hint@example.com'
if (!existsSync(mainEntry)) {
console.error(`[gmail-smoke] ${mainEntry} missing — run: npx electron-vite build`)
process.exit(1)
}
const baseEnv = {
...process.env,
OMI_E2E: '1',
OMI_E2E_FAKE_AUTH: '1',
OMI_AUTOMATION: '0',
OMI_SKIP_TUNNEL: '1'
}
const SECONDARY = ['#/bar', '#/insight', '#/notch', '#/capture', '#/glow']
const isSecondary = (url) => SECONDARY.some((h) => url.includes(h))
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
// Read every BrowserWindow's URL from the MAIN process (robust for the sandboxed
// Google window, which Playwright may not attach a page to).
const windowUrls = (app) =>
app.evaluate(({ BrowserWindow }) =>
BrowserWindow.getAllWindows().map((w) => {
try {
return w.webContents.getURL()
} catch {
return ''
}
})
)
const googleWindowInfo = (app) =>
app.evaluate(async ({ BrowserWindow }) => {
const w = BrowserWindow.getAllWindows().find((win) =>
(win.webContents.getURL() || '').includes('google.com')
)
if (!w) return null
const url = w.webContents.getURL()
const title = w.getTitle()
const bodyText = await w.webContents
.executeJavaScript('document.body ? document.body.innerText.slice(0, 800) : ""')
.catch(() => '')
const png = await w.webContents
.capturePage()
.then((img) => img.toDataURL())
.catch(() => null)
return { url, title, bodyText, png }
})
async function mainPage(app) {
for (let i = 0; i < 150; i++) {
const page = (await app.windows()).find((w) => !isSecondary(w.url()))
if (page) {
// Evaluate the readiness predicate INSIDE the page — a function reference can't
// cross Playwright's serialization boundary, so return a boolean, not the fn.
const ready = await page
.evaluate(
() =>
(document.querySelector('#root')?.childElementCount ?? 0) > 0 &&
typeof window.omi?.gmailSessionConnect === 'function'
)
.catch(() => false)
if (ready) return page
}
await sleep(100)
}
throw new Error('main window / window.omi.gmailSessionConnect never became ready')
}
async function main() {
mkdirSync(shotsDir, { recursive: true })
const userDataDir = mkdtempSync(path.join(tmpdir(), 'omi-gmail-smoke-'))
const app = await electron.launch({
args: [mainEntry, `--user-data-dir=${userDataDir}`],
env: baseEnv
})
let pass = false
try {
const page = await mainPage(app)
console.log('[gmail-smoke] main window ready; triggering gmailSessionConnect()')
// Fire the connect flow (opens the login window). Do NOT await — it resolves only
// on login / window-close / timeout. Stash the promise so --login can read it.
// Pass a (fake) email so we can also prove the login_hint arg flows renderer->main.
await page.evaluate((email) => {
window.__gmailConnect = window.omi.gmailSessionConnect(email)
}, SMOKE_LOGIN_HINT)
// Poll for the login window to reach accounts.google.com.
let info = null
for (let i = 0; i < 90; i++) {
const urls = await windowUrls(app)
if (urls.some((u) => u.includes('google.com'))) {
info = await googleWindowInfo(app)
if (info && info.url) break
}
await sleep(500)
}
if (!info) {
console.error('[gmail-smoke] FAIL — no google.com login window appeared within ~45s')
console.error('[gmail-smoke] open window URLs:', await windowUrls(app))
} else {
console.log('[gmail-smoke] login window URL :', info.url)
console.log('[gmail-smoke] login window title:', info.title)
// login_hint is verified authoritatively by the buildGmailLoginUrl unit test.
// Google consumes it during the redirect to /v3/signin/identifier, so its absence
// from this post-redirect URL is expected — this is informational only.
const hintSeen = info.url.includes('login_hint=')
console.log(
`[gmail-smoke] login_hint in observed (post-redirect) URL: ${hintSeen ? 'yes' : 'no (consumed by Google — expected; unit test covers the built URL)'}`
)
const reachedGoogle = /(^https:\/\/accounts\.google\.com)|(\.google\.com)/.test(info.url)
const blocked =
/couldn't sign you in|this browser or app may not be secure|disallowed_useragent/i.test(
`${info.title}\n${info.bodyText}`
)
if (info.png) {
const file = path.join(shotsDir, 'login-window.png')
writeFileSync(file, Buffer.from(info.png.split(',')[1], 'base64'))
console.log('[gmail-smoke] screenshot:', file)
}
if (blocked) {
console.error('[gmail-smoke] WARNING — Google served an embedded-UA block page:')
console.error(info.bodyText.slice(0, 300))
} else {
console.log('[gmail-smoke] no embedded-UA block detected on the sign-in page')
}
pass = reachedGoogle && !blocked
}
if (LOGIN_MODE && info) {
console.log('\n[gmail-smoke] --login: sign into Google in the window. Waiting up to 5 min…')
const connected = await page
.evaluate(() => window.__gmailConnect)
.catch(() => ({ connected: false, message: 'connect promise rejected' }))
console.log('[gmail-smoke] connect resolved:', JSON.stringify(connected))
if (connected && connected.connected) {
const res = await page.evaluate(() => window.omi.gmailSessionFetch('newer_than:7d', 25))
console.log(
'[gmail-smoke] fetch result:',
JSON.stringify({
ok: res.ok,
count: res.emails?.length,
source: res.source,
error: res.error
})
)
pass = res.ok
} else {
pass = false
}
} else if (info) {
// Default mode: close the login window without signing in (resolves connect to
// not-connected) to prove teardown, then finish.
await app.evaluate(({ BrowserWindow }) => {
const w = BrowserWindow.getAllWindows().find((win) =>
(win.webContents.getURL() || '').includes('google.com')
)
if (w && !w.isDestroyed()) w.close()
})
const resolved = await page.evaluate(() => window.__gmailConnect).catch(() => null)
console.log('[gmail-smoke] connect resolved after close:', JSON.stringify(resolved))
}
} finally {
// app.close() can hang on Windows after a child BrowserWindow existed; don't let
// teardown mask a completed verification — race it against a short timeout.
await Promise.race([app.close().catch(() => {}), sleep(8000)])
}
console.log(`\n[gmail-smoke] ${pass ? 'PASS' : 'FAIL'}`)
process.exit(pass ? 0 : 1)
}
main().catch((e) => {
console.error('[gmail-smoke] error:', e)
process.exit(1)
})