Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions scripts/test/browser-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function create_browser_runtime(): Promise<Runtime> {
'dhtml/client': '/dist/client.js',
'dhtml/server': '/dist/server.js',
birpc: '/node_modules/birpc/dist/index.mjs',
devalue: '/node_modules/devalue/index.js',
capnweb: '/node_modules/capnweb/dist/index.js',
mitata: '/node_modules/mitata/src/main.mjs',
},
})}</script>
Expand Down Expand Up @@ -82,7 +82,7 @@ export async function create_browser_runtime(): Promise<Runtime> {
}
})
const { port1, port2 } = new MessageChannel()
await page.exposeFunction('__postMessage', (data: any) => port1.postMessage(data))
await page.exposeFunction('__postMessage', (data: string) => port1.postMessage(data))

await page.coverage.startJSCoverage({ includeRawScriptCoverage: true })
await page.goto(`http://${addr}/@runner`)
Expand Down
14 changes: 0 additions & 14 deletions scripts/test/devalue.ts

This file was deleted.

17 changes: 5 additions & 12 deletions scripts/test/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { createBirpc } from 'birpc'
import { newMessagePortRpcSession } from 'capnweb'
import * as fs from 'node:fs/promises'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
import { parseArgs, styleText } from 'node:util'
import { create_browser_runtime } from './browser-runtime.ts'
import { handle_coverage, type Coverage } from './coverage.ts'
import * as devalue from './devalue.ts'
import { create_node_runtime } from './node-runtime.ts'
import type { ClientFunctions, TestResult } from './runtime.ts'

Expand Down Expand Up @@ -45,7 +44,8 @@ for (const [runtime, files] of Object.entries(all_files)) {
const rt = runtime === 'node' ? await create_node_runtime() : await create_browser_runtime()
await using _ = rt // workaround for https://issues.chromium.org/issues/409478039

const client = createBirpc<ClientFunctions, ServerFunctions>(
using client = newMessagePortRpcSession<ClientFunctions>(
rt.port,
{
report_result(run) {
if (run.result === 'pass') {
Expand All @@ -58,15 +58,8 @@ for (const [runtime, files] of Object.entries(all_files)) {

results.push(run)
},
},
{
post: data => rt.port.postMessage(data),
on: fn => {
rt.port.onmessage = e => fn(e.data)
},
serialize: devalue.stringify,
deserialize: devalue.parse,
},
} satisfies ServerFunctions,
{ onSendError: e => e },
)

await client.define('__DEV__', !args.values.prod)
Expand Down
3 changes: 1 addition & 2 deletions scripts/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"@types/istanbul-reports": "^3.0.4",
"amaro": "^1.1.0",
"ast-v8-to-istanbul": "^0.3.3",
"birpc": "^2.5.0",
"devalue": "^5.1.1",
"capnweb": "^0.1.0",
"hono": "^4.8.5",
"istanbul-lib-coverage": "^3.2.2",
"istanbul-lib-report": "^3.0.1",
Expand Down
37 changes: 15 additions & 22 deletions scripts/test/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createBirpc } from 'birpc'
import { newMessagePortRpcSession, RpcStub } from 'capnweb'
import * as mitata from 'mitata'
import * as devalue from './devalue.ts'
import type { ServerFunctions } from './main.ts'

export type TestResult = { name: string } & ({ result: 'pass'; duration: number } | { result: 'fail'; reason: unknown })
Expand All @@ -17,8 +16,8 @@ const client: ClientFunctions = {
define(name, value) {
globalThis[name] = value
},
import(path) {
return import(path)
async import(path) {
return new RpcStub((await import(path)) as object)
},
async run_tests(options) {
for (const test of tests) {
Expand Down Expand Up @@ -47,25 +46,19 @@ const client: ClientFunctions = {
}

declare global {
var __onmessage: (fn: (data: any) => void) => void
var __postMessage: (data: any) => void
var __onmessage: (fn: (data: string) => void) => void
var __postMessage: (data: string) => void
}

const server = createBirpc<ServerFunctions, ClientFunctions>(
client,
typeof process === 'undefined'
? {
post: window.__postMessage,
on: fn => (window.__onmessage = fn),
serialize: devalue.stringify,
deserialize: devalue.parse,
}
: {
post: data => process.send!(data),
on: fn => process.on('message', fn),
serialize: devalue.stringify,
deserialize: devalue.parse,
},
)
const { port1, port2 } = new MessageChannel()
if (typeof process === 'undefined') {
window.__onmessage = data => port1.postMessage(data)
port1.onmessage = event => window.__postMessage(event.data)
} else {
process.on('message', data => port1.postMessage(data))
port1.onmessage = event => process.send!(event.data)
}

const server = newMessagePortRpcSession<ServerFunctions>(port2, client, { onSendError: e => e })

export const tests: Array<{ name: string; fn: () => void | Promise<void> }> = []