import assert from 'node:assert/strict'; import { readFile, writeFile } from 'node:fs/promises'; import { launchBrowser } from './launch-browser.mjs'; const origin = process.argv[2] ?? 'http://localhost:4341'; const browser = await launchBrowser(); const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); page.setDefaultTimeout(60000); const sql = []; page.on('console', (message) => { if (message.text().startsWith('SQL ')) { sql.push(message.text()); } }); const _edit = (filename, value) => page.evaluate( ({ filename, value }) => { const model = window.monaco.editor .getModels() .find((model) => model.uri.path.endsWith(`/${filename}`)); if (!model) { throw new Error(`Missing editor model ${filename}`); } model.setValue(value); }, { filename, value }, ); const edit = (filename, value) => page.evaluate( ({ filename, value }) => { window.monaco.editor .getModel(window.monaco.Uri.parse(`file:///playground/${filename}`)) .setValue(value); }, { filename, value }, ); const run = async () => { await page.getByRole('button', { name: /^Run query/ }).click(); await page.getByRole('button', { name: /^Run query/ }).waitFor(); const output = page.getByLabel('GraphQL result').first(); await output.filter({ hasText: /\S/ }).waitFor({ state: 'attached' }); return JSON.parse(await output.textContent()); }; // Inspect the same Monaco content a reader sees, through the generic response tabs. const inspectQueries = async (surface = page) => { await surface.getByRole('button', { name: 'Queries', exact: true }).click(); const tabs = surface.getByRole('tablist', { name: 'Queries sub-tabs' }).getByRole('tab'); const names = await tabs.allTextContents(); const entries = []; for (const name of names) { await tabs.filter({ hasText: name }).click(); const method = name.replace(/^\d+\. /, ''); await surface.waitForFunction( (method) => window.monaco.editor.getEditors().some((editor) => { if ( editor.getDomNode()?.offsetParent === null || !editor.getOption(window.monaco.editor.EditorOption.readOnly) ) { return false; } try { return JSON.parse(editor.getValue()).method === method; } catch { return false; } }), method, ); entries.push( await surface.evaluate((method) => { for (const editor of window.monaco.editor.getEditors()) { if ( editor.getDomNode()?.offsetParent === null || !editor.getOption(window.monaco.editor.EditorOption.readOnly) ) { continue; } try { const value = JSON.parse(editor.getValue()); if (value.method === method) { return value; } } catch {} } }, method), ); } await surface.getByRole('button', { name: 'Response', exact: true }).click(); return entries; }; try { await page.goto(`${origin}/playground?example=plugin-drizzle`); await page.getByRole('button', { name: /schema synced/ }).waitFor(); await page.waitForFunction(() => window.monaco?.languages.typescript); await page.waitForTimeout(2000); const diagnostics = await page.evaluate(async () => { const monaco = window.monaco; const models = monaco.editor .getModels() .filter((model) => model.getLanguageId() === 'typescript'); const worker = await (await monaco.languages.typescript.getTypeScriptWorker())( ...models.map((model) => model.uri), ); const errors = []; for (const model of models) { for (const diagnostic of [ ...(await worker.getSyntacticDiagnostics(model.uri.toString())), ...(await worker.getSemanticDiagnostics(model.uri.toString())), ]) { if (diagnostic.category === 1) { errors.push({ file: model.uri.path, code: diagnostic.code, message: diagnostic.messageText, }); } } } return errors; }); assert.deepEqual(diagnostics, [], 'editor types'); const bundle = JSON.parse( await readFile( new URL('../public/playground-examples/plugin-drizzle.json', import.meta.url), 'utf8', ), ); const results = {}; for (const query of bundle.queries) { await page .getByRole('tablist', { name: 'Operations' }) .getByRole('tab', { name: query.title, exact: true }) .click(); sql.length = 0; const result = await run(); assert.equal(result.errors, undefined, JSON.stringify(result.errors)); results[`${query.title}.graphql`] = result; if (query.title === '01-author') { assert.equal(sql.length, 1, 'nested author and posts are loaded by one SQL query'); const entries = await inspectQueries(); assert.equal(entries.length, 1); assert.equal(entries[0].method, 'db.query.users.findFirst'); assert.deepEqual(entries[0].arguments[0].where, { id: 1 }); assert.ok(entries[0].arguments[0].with.posts, 'show the nested relation options'); } if (query.title === '02-aliases') { assert.equal(sql.length, 2, 'incompatible aliases require a fallback query'); assert.deepEqual(result.data.author.newest, [...result.data.author.oldest].reverse()); const entries = await inspectQueries(); assert.deepEqual( entries.map((entry) => entry.method), ['db.query.users.findFirst', 'db.query.users.findMany'], ); assert.equal( entries[1].arguments[0].where.RAW.$type, 'Function', 'fallback callback is represented without evaluating it', ); } console.log('PASS', query.title); } if (process.env.UPDATE_DATABASE_EXPECTATIONS === '1') { await writeFile( new URL('../playground-examples/plugin-drizzle/expected.json', import.meta.url), `${JSON.stringify(results, null, 2)}\n`, ); } else { assert.deepEqual( results, JSON.parse( await readFile( new URL('../playground-examples/plugin-drizzle/expected.json', import.meta.url), 'utf8', ), ), ); } const operation = async (title) => { await page .getByRole('tablist', { name: 'Operations' }) .getByRole('tab', { name: title, exact: true }) .click(); }; const pane = async (name, text) => { await page .getByRole('tablist', { name: 'Operation panes' }) .getByRole('tab', { name: new RegExp(`^${name}`) }) .click(); await page.waitForFunction( (language) => window.monaco.editor .getEditors() .some( (editor) => editor.getModel()?.getLanguageId() === language && editor.getDomNode()?.offsetParent !== null && !editor.getOption(window.monaco.editor.EditorOption.readOnly), ), name === 'Query' ? 'graphql' : 'json', ); await page.evaluate( ({ language, text }) => { const editor = window.monaco.editor .getEditors() .find( (editor) => editor.getModel()?.getLanguageId() === language && editor.getDomNode()?.offsetParent !== null && !editor.getOption(window.monaco.editor.EditorOption.readOnly), ); editor.getModel().setValue(text); }, { language: name === 'Query' ? 'graphql' : 'json', text }, ); await page.waitForTimeout(250); }; await operation('04-pagination'); const first = results['04-pagination.graphql'].data.posts; await pane('Variables', JSON.stringify({ after: first.pageInfo.endCursor })); const next = await run(); assert.equal(next.errors, undefined); assert.deepEqual( next.data.posts.nodes.map((post) => post.title), ['Starting a seed library'], ); assert.equal(next.data.posts.pageInfo.hasNextPage, false); await pane( 'Query', `query { posts(last: 1, before: "${first.pageInfo.endCursor}") { nodes { title } } }`, ); assert.deepEqual((await run()).data.posts.nodes, [{ title: 'Watering through summer' }]); await pane( 'Query', `query { posts(first: 2, after: "${next.data.posts.pageInfo.endCursor}") { nodes { title } pageInfo { hasNextPage } } }`, ); assert.deepEqual((await run()).data.posts.nodes, []); await pane('Query', '{ author(id: 1) { postsConnection { totalCount } } }'); sql.length = 0; assert.deepEqual((await run()).data, { author: { postsConnection: { totalCount: 2 } } }); assert.ok( sql.length > 0 && sql.some((statement) => /count\(/i.test(statement)), 'count-only selection reaches SQL', ); await pane('Query', '{ posts { totalCount } }'); sql.length = 0; assert.deepEqual((await run()).data, { posts: { totalCount: 3 } }); assert.equal(sql.length, 1, 'root count-only selection skips the row query'); assert.match(sql[0], /count\(/i); const countEntries = await inspectQueries(); assert.equal(countEntries.length, 1, 'capture is replaced on every run'); assert.equal(countEntries[0].method, 'db.$count'); assert.equal(countEntries[0].arguments[1].$type, 'SQL'); await pane('Query', '{ __typename }'); await run(); assert.equal( await page.getByRole('button', { name: 'Queries', exact: true }).count(), 0, 'no stale Queries tab for a run without database calls', ); await operation('10-attachments'); const attachments = results['10-attachments.graphql'].data.author.postsConnection.nodes[0].attachments; assert.equal(attachments.edges.length, 2); assert.equal(attachments.pageInfo.hasNextPage, true); assert.equal(attachments.edges[0].node.uploadedBy.fullName, 'Leo Silva'); await pane('Variables', JSON.stringify({ after: attachments.pageInfo.endCursor })); const attachmentNext = (await run()).data.author.postsConnection.nodes[0].attachments; assert.equal(attachmentNext.edges.length, 1); assert.equal(attachmentNext.edges[0].caption, null); assert.equal(attachmentNext.pageInfo.hasNextPage, false); await pane('Variables', JSON.stringify({ hasCaption: true })); const captioned = (await run()).data.author.postsConnection.nodes[0].attachments; assert.equal(captioned.edges.length, 2); assert.equal(captioned.pageInfo.hasNextPage, false); assert.ok(captioned.edges.every((edge) => typeof edge.caption === 'string')); await pane( 'Query', `{ author(id: 1) { postsConnection(first: 1) { nodes { attachments(last: 1, before: "${attachmentNext.pageInfo.startCursor}") { edges { caption node { url } } } } } } }`, ); const previousAttachment = (await run()).data.author.postsConnection.nodes[0].attachments; assert.equal(previousAttachment.edges[0].caption, attachments.edges[1].caption); await pane( 'Query', `{ author(id: 1) { postsConnection(first: 1) { nodes { attachments(first: 2) { edges { caption } } } } } }`, ); const edgeOnly = await run(); assert.equal(edgeOnly.errors, undefined); assert.deepEqual( edgeOnly.data.author.postsConnection.nodes[0].attachments.edges, attachments.edges.map(({ caption }) => ({ caption })), ); console.log('PASS root count-only and attachment forward/backward/filtered pagination'); await operation('03-viewer'); await pane('Context', '{"userId":2}'); const leo = await run(); assert.equal(leo.data.me.__typename, 'AuthorViewer'); assert.deepEqual(leo.data.me.drafts, [{ title: 'Saving rainwater' }]); await pane('Context', '{"userId":999}'); assert.deepEqual((await run()).data, { me: null }); await operation('09-variant'); await pane('Context', '{"userId":2}'); const variant = await run(); assert.equal(variant.data.own.viewer, null); assert.equal(variant.data.other.viewer.email, 'leo@example.com'); await operation('01-author'); const database = bundle.files.find((file) => file.filename === 'database.ts').content; await page.getByRole('button', { name: 'database.ts', exact: true }).click(); await edit('database.ts', database.replace('Maya', 'Amaya')); await page.waitForTimeout(1000); await page.getByRole('button', { name: /schema synced/ }).waitFor(); assert.equal((await run()).data.author.fullName, 'Amaya Chen'); await page.context().grantPermissions(['clipboard-read', 'clipboard-write']); await page.getByRole('button', { name: 'Share', exact: true }).click(); await page.getByRole('button', { name: 'Copied!', exact: true }).waitFor(); const sharedUrl = await page.evaluate(() => navigator.clipboard.readText()); const shared = await browser.newPage(); shared.setDefaultTimeout(60000); try { await shared.goto(sharedUrl); await shared.getByRole('button', { name: 'Trust and build schema', exact: true }).click(); await shared.getByRole('button', { name: /schema synced/ }).waitFor(); await shared.getByRole('button', { name: /^Run query/ }).click(); await shared .getByLabel('GraphQL result') .first() .filter({ hasText: 'Amaya Chen' }) .waitFor({ state: 'attached' }); } finally { await shared.close(); } await page.getByRole('button', { name: 'Reset example', exact: true }).click(); await page.waitForTimeout(1000); await page.getByRole('button', { name: /schema synced/ }).waitFor(); assert.equal((await run()).data.author.fullName, 'Maya Chen'); assert.equal( (await inspectQueries()).length, 1, 'reset does not retain captures from an old database', ); console.log( 'PASS cursor continuation, backward/empty pages, count-only SQL, viewer isolation, seed edit and reset', ); await page.getByRole('button', { name: 'types/user.ts', exact: true }).click(); const schemaSource = bundle.files.find((file) => file.filename === 'types/user.ts').content; await edit( 'types/user.ts', schemaSource.replace("t.exposeString('firstName')", "t.exposeString('notAColumn')"), ); await page.waitForFunction(async () => { const monaco = window.monaco; const uri = monaco.Uri.parse('file:///playground/types/user.ts'); const worker = await (await monaco.languages.typescript.getTypeScriptWorker())(uri); return (await worker.getSemanticDiagnostics(uri.toString())).some( (diagnostic) => diagnostic.code === 2345 || diagnostic.code === 2322, ); }); await page.getByRole('button', { name: 'Reset example', exact: true }).click(); await page.waitForTimeout(1000); await page.getByRole('button', { name: /schema synced/ }).waitFor(); console.log('PASS shared edited database and unknown-column type rejection'); await page.screenshot({ path: '/tmp/pothos-drizzle-browser.png' }); for (const [route, checks] of [ ['', ['author']], ['objects', ['author', 'author']], ['selections', ['author', 'profile', 'computed']], ['relations', ['author', 'count', 'media']], ['variants', ['viewer', 'viewer', 'variant']], ['interfaces', ['viewer', 'editor']], ['query-planning', ['aliases']], ['connections', ['feed', 'related']], ['connection-helpers', ['attachments', 'captioned-attachments']], ['ordering-and-cursors', ['posts']], ]) { const docs = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); docs.setDefaultTimeout(60000); try { const source = await readFile( new URL(`../content/docs/plugins/drizzle/${route || 'index'}.mdx`, import.meta.url), 'utf8', ); const excerpts = [ ...source.matchAll( /]*meta='[^']*playground[^']*'[^>]*>playground-examples\/plugin-drizzle\/([^<#]+)#[^<]+<\/include>/g, ), ].map((match) => match[1]); assert.equal(excerpts.length, checks.length, `${route}: check every rendered action`); await docs.goto(`${origin}/docs/plugins/drizzle${route ? `/${route}` : ''}`); for (const [index, check] of checks.entries()) { await docs .getByRole('button', { name: 'Open in Playground', exact: true }) .nth(index) .click(); const frame = docs.frameLocator('iframe[title="Pothos Playground"]'); await frame.getByRole('button', { name: /schema synced/ }).waitFor(); const runtime = docs.frames().find((candidate) => candidate.url().includes('/playground?')); await runtime.waitForFunction( (filename) => window.monaco.editor.getEditors().some( (editor) => editor.getModel()?.uri.path === `/playground/${filename}` && editor.getDomNode()?.offsetParent !== null && editor .getModel() .getAllDecorations() .some( (decoration) => decoration.options.className === 'playground-source-highlight' && editor .getVisibleRanges() .some( (range) => range.startLineNumber <= decoration.range.startLineNumber && range.endLineNumber >= decoration.range.startLineNumber, ), ), ), excerpts[index], ); await frame.getByRole('button', { name: /^Run query/ }).click(); const output = frame.getByLabel('GraphQL result').first(); await output .filter({ hasText: /Maya|composting|EditorViewer|maya/ }) .waitFor({ state: 'attached' }); const result = JSON.parse(await output.textContent()); assert.equal(result.errors, undefined); if (check === 'computed') { assert.equal(result.data.author.lowercaseName, 'maya'); } if (check === 'profile') { assert.equal(result.data.missingProfile.bio, null); } if (check === 'count') { assert.equal(result.data.author.postCount, 2); assert.equal(result.data.empty.postCount, 0); } if (check === 'variant') { assert.equal(result.data.other.viewer, null); assert.equal(result.data.own.viewer.email, 'maya@example.com'); } if (check === 'editor') { assert.equal(result.data.me.canReviewSubmissions, true); } if (check === 'related') { assert.equal(result.data.author.postsConnection.totalCount, 2); assert.equal(result.data.author.postsConnection.nodes.length, 1); } if (check === 'aliases') { assert.deepEqual(result.data.author.newest, [...result.data.author.oldest].reverse()); const entries = await inspectQueries(runtime); assert.deepEqual( entries.map((entry) => entry.method), ['db.query.users.findFirst', 'db.query.users.findMany'], ); assert.equal( entries[1].arguments[0].where.RAW.$type, 'Function', 'fallback callback is represented without evaluating it', ); } if (check === 'viewer') { assert.equal(result.data.me.__typename, 'EditorViewer'); } if (check === 'feed') { assert.equal(result.data.posts.totalCount, 3); assert.equal(result.data.posts.edges.length, 2); assert.ok( result.data.posts.edges.every((edge) => edge.cursor && edge.node.author.fullName), ); } if (check === 'attachments' || check === 'captioned-attachments') { const connection = result.data.author.postsConnection.nodes[0].attachments; assert.equal(connection.edges.length, 2); assert.equal(connection.pageInfo.hasNextPage, check === 'attachments'); assert.ok(connection.edges.every((edge) => edge.caption)); } if (check === 'posts') { assert.equal(result.data.posts.nodes.length, 2); } if (check === 'author') { assert.equal(result.data.author.fullName, 'Maya Chen'); } if (check === 'media') { assert.equal(result.data.author.posts.length, 2); for (const post of result.data.author.posts) { assert.equal(post.media[0].uploadedBy.fullName, 'Leo Silva'); assert.equal( post.mediaConnection.totalCount, post.title === 'A guide to composting' ? 3 : 1, ); } } await docs.getByRole('button', { name: 'Close playground', exact: true }).click(); } console.log('PASS rendered actions', route); } finally { await docs.close(); } } await page.setViewportSize({ width: 390, height: 844 }); await page.goto(`${origin}/docs/plugins/drizzle/objects`); await page.getByRole('button', { name: 'Open in Playground', exact: true }).first().click(); const mobile = page.frameLocator('iframe[title="Pothos Playground"]'); await mobile.getByRole('button', { name: /schema synced/ }).waitFor(); const runButton = mobile.getByRole('button', { name: /^Run query/ }); const bounds = await runButton.boundingBox(); assert.ok(bounds && bounds.x >= 0 && bounds.x + bounds.width <= 390); await runButton.click(); await mobile .getByLabel('GraphQL result') .first() .filter({ hasText: 'Maya Chen' }) .waitFor({ state: 'attached' }); const mobileRuntime = page.frames().find((candidate) => candidate.url().includes('/playground?')); assert.equal((await inspectQueries(mobileRuntime)).length, 1); await mobile.getByRole('button', { name: 'Queries', exact: true }).click(); await mobile.getByRole('tablist', { name: 'Queries sub-tabs' }).scrollIntoViewIfNeeded(); await page.screenshot({ path: '/tmp/pothos-drizzle-mobile.png' }); console.log('PASS mobile docs action, query and result'); } finally { await browser.close(); }