-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompat.js
74 lines (64 loc) · 1.98 KB
/
compat.js
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
import fs from 'node:fs'
import path from 'node:path'
import sade from 'sade'
import runner from 'entail'
import { test } from './tests.js'
import { API } from './api.js'
/**
* @typedef {{ client: import('./api.js').API, server: import('./api.js').API }} Context
* @typedef {{ service: { id: import('@ipld/dag-ucan').DID, url: URL } }} ServiceContext
* @typedef {(assert: import('entail').Assert, ctx: Context & ServiceContext) => unknown} Test
*/
const { dirname } = import.meta
const pkg = JSON.parse(fs.readFileSync(path.join(dirname, 'package.json'), 'utf-8'))
/**
* @param {Context} ctx
* @param {(assert: import('entail').Assert, ctx: Context) => unknown} fn
*/
const withContext = (ctx, fn) => {
return (/** @type {import('entail').Assert} */ assert) => {
return fn(assert, { ...ctx })
}
}
/**
* @template {Context} C
* @param {(assert: import('entail').Assert, ctx: C & ServiceContext) => unknown} fn
*/
const withServiceContext = fn => {
return async (
/** @type {import('entail').Assert} */ assert,
/** @type {C} */ context
) => {
let service
try {
service = await context.server.startService()
return await fn(assert, { ...context, service })
} finally {
if (service) await context.server.stopService(service.id)
}
}
}
const prog = sade(pkg.name)
prog
.version(pkg.version)
prog
.command('test <client> <server>')
.describe('Test compatibility between client and server implementations.')
.example('test go js')
.example('test go js')
.action(async (clientLang, serverLang) => {
const client = new API(path.join(dirname, clientLang))
const server = new API(path.join(dirname, serverLang))
const ctx = { client, server }
const suite = Object.fromEntries(
[...Object.entries(test)].map(([k, v]) => (
[k, withContext(ctx, withServiceContext(v))]
))
)
await runner({
[`Compatibility ${clientLang} → ${serverLang}`]: {
test: suite
}
})
})
prog.parse(process.argv)