@sssb/kit 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/release.json +1 -1
- package/package.json +3 -1
- package/src/adapters/auth-tenancy.ts +157 -0
- package/src/adapters/configured-auth.ts +183 -0
- package/src/adapters/email-message.ts +30 -0
- package/src/adapters/fake/analytics.ts +1 -1
- package/src/adapters/fake/auth.ts +53 -3
- package/src/adapters/fake/data-dir.ts +32 -2
- package/src/adapters/fake/email.ts +2 -0
- package/src/adapters/fake/jobs.ts +5 -4
- package/src/adapters/fake/simulated-auth.ts +24 -0
- package/src/adapters/index.ts +3 -2
- package/src/adapters/real/accept-invite-form.tsx +32 -14
- package/src/adapters/real/analytics.ts +29 -2
- package/src/adapters/real/auth.ts +127 -23
- package/src/adapters/real/contract-postgres.ts +15 -11
- package/src/adapters/real/email.ts +2 -0
- package/src/adapters/real/index.ts +19 -3
- package/src/adapters/real/jobs-in-process.ts +32 -14
- package/src/adapters/real/jobs.ts +6 -3
- package/src/adapters/real/migrate-handler.ts +3 -1
- package/src/adapters/real/rds-data-codec.ts +88 -4
- package/src/adapters/real/rds-data-emulator.ts +12 -4
- package/src/adapters/real/sign-in-form.tsx +29 -13
- package/src/authz/app-exemptions.ts +79 -9
- package/src/authz/authorize.ts +4 -1
- package/src/authz/route-scan.ts +626 -55
- package/src/components/auth/accept-invite-screen.tsx +5 -1
- package/src/components/auth/people-picker.tsx +20 -2
- package/src/components/profile-screen.tsx +27 -2
- package/src/core/abilities.ts +22 -6
- package/src/core/inbound-email.ts +41 -6
- package/src/core/jobs.ts +17 -0
- package/src/core/roles.ts +17 -2
- package/src/cutover/rows.json +4 -0
- package/src/db/jobs.ts +32 -11
- package/src/db/keyset.ts +8 -6
- package/src/db/migrations/0015_app_user_grant.ts +1 -1
- package/src/db/migrations/0017_job_requester.ts +20 -0
- package/src/db/migrations/index.ts +2 -0
- package/src/db/rls-coverage.ts +51 -17
- package/src/db/schedules.ts +9 -3
- package/src/db/schema.ts +2 -0
- package/src/demo-static/actor-drivers.ts +8 -2
- package/src/demo-static/contracts.ts +9 -0
- package/src/demo-static/email-html.ts +9 -0
- package/src/demo-static/rows.ts +2 -0
- package/src/demo-static/tour/use-tours.tsx +8 -2
- package/src/demo-static/world.ts +98 -41
- package/src/email/send.ts +6 -2
- package/src/guide.md +37 -6
- package/src/i18n/messages/en.json +3 -0
- package/src/i18n/messages/es.json +3 -0
- package/src/i18n/messages.ts +3 -2
- package/src/ports/auth.ts +10 -1
- package/src/server-lib/notify.ts +51 -9
- package/src/server-lib/scheduled-tick.ts +5 -1
- package/src/server-lib/webhook-dispatch.ts +3 -0
- package/src/service-auth/mailgun.ts +19 -4
- package/src/testing/conformance/auth-provider.ts +17 -5
- package/src/testing/conformance/authz.ts +6 -2
- package/src/testing/conformance/demo-presets.ts +4 -4
- package/src/testing/conformance/i18n.ts +99 -5
- package/src/testing/conformance/suite.ts +63 -3
|
@@ -39,16 +39,21 @@ function signingKey(): string | null {
|
|
|
39
39
|
return isSimulated ? devWebhookSecret() : (process.env['MAILGUN_WEBHOOK_SIGNING_KEY'] ?? null)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* token -> expiry epoch ms; a token is a replay if seen while still within the window. The expiry is
|
|
44
|
+
* the moment its SIGNATURE stops being fresh, not "tolerance from now": the timestamp may run up to the
|
|
45
|
+
* tolerance AHEAD of our clock (verifyMailgunSignature checks both directions), so a token remembered
|
|
46
|
+
* for only `now + tolerance` would be forgotten while a replay of it still verified.
|
|
47
|
+
*/
|
|
43
48
|
const seenTokens = new Map<string, number>()
|
|
44
49
|
|
|
45
|
-
function rememberToken(token: string, nowMs: number): boolean {
|
|
50
|
+
function rememberToken(token: string, nowMs: number, timestampMs: number): boolean {
|
|
46
51
|
// Prune expired entries first so the map can't grow unbounded across a long-lived process.
|
|
47
52
|
for (const [seen, expiry] of seenTokens) {
|
|
48
53
|
if (expiry <= nowMs) seenTokens.delete(seen)
|
|
49
54
|
}
|
|
50
55
|
if (seenTokens.has(token)) return false
|
|
51
|
-
seenTokens.set(token, nowMs + WEBHOOK_REPLAY_TOLERANCE_MS)
|
|
56
|
+
seenTokens.set(token, Math.max(nowMs, timestampMs) + WEBHOOK_REPLAY_TOLERANCE_MS)
|
|
52
57
|
return true
|
|
53
58
|
}
|
|
54
59
|
|
|
@@ -61,7 +66,17 @@ export function verifyMailgunWebhook(fields: MailgunSignatureFields, now: Date =
|
|
|
61
66
|
if (!key) throw new AuthRequiredError()
|
|
62
67
|
const result = verifyMailgunSignature({ ...fields, signingKey: key, toleranceMs: WEBHOOK_REPLAY_TOLERANCE_MS, now })
|
|
63
68
|
if (!result.ok) throw new AuthRequiredError()
|
|
64
|
-
if (!rememberToken(fields.token, now.getTime())) throw new AuthRequiredError()
|
|
69
|
+
if (!rememberToken(fields.token, now.getTime(), Number(fields.timestamp) * 1000)) throw new AuthRequiredError()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Forget a token whose request was verified but NOT processed: the route's handler threw or answered 5xx.
|
|
74
|
+
* The sender's retry of that message carries the same token, and refusing it as a replay would lose the
|
|
75
|
+
* message. A replay of an unprocessed message is a retry, not a second delivery, so forgetting it opens
|
|
76
|
+
* nothing the timestamp window does not already bound.
|
|
77
|
+
*/
|
|
78
|
+
export function forgetMailgunToken(token: string): void {
|
|
79
|
+
seenTokens.delete(token)
|
|
65
80
|
}
|
|
66
81
|
|
|
67
82
|
/** Test-only: clear the replay set between cases so a reused fixture token doesn't leak across tests. */
|
|
@@ -14,7 +14,7 @@ import type { CheckImpl } from './types'
|
|
|
14
14
|
* How it reads the app: from each of Next's rendering files under `src/app` (`page`, `layout`, `template`, `default`,
|
|
15
15
|
* `not-found`, `error`, `global-error`, `loading`, `forbidden`, `unauthorized`), it follows the app's own imports
|
|
16
16
|
* (a relative path, `@/` for the app's `src/`, `@sssb/app-config/` for its seam) through import and export
|
|
17
|
-
* declarations and `import()` of a literal, and notes whether each file reaches a form or the provider. An import that
|
|
17
|
+
* declarations, `import x = require()`, and `import()` or `require()` of a literal, and notes whether each file reaches a form or the provider. An import that
|
|
18
18
|
* loads nothing (`import type`, or every name marked `type`) is not followed, nor is a specifier it cannot resolve to
|
|
19
19
|
* a file; a comment or a string naming a module does not count. A rendering file that reaches a form passes when it
|
|
20
20
|
* is wrapped by one that reaches the provider: a layout or template that is itself that file, or one in a directory
|
|
@@ -22,8 +22,9 @@ import type { CheckImpl } from './types'
|
|
|
22
22
|
* inside neither. `global-error` replaces the root layout, so only it can wrap itself.
|
|
23
23
|
*
|
|
24
24
|
* What it does not read: whether an import runs (an `isSimulated` branch is how it should be written, and a dead one
|
|
25
|
-
* would pass), a path alias other than those two,
|
|
26
|
-
* authorization scan does not
|
|
25
|
+
* would pass), a path alias other than those two, anything behind a symbolic link, which it does not follow, as the
|
|
26
|
+
* authorization scan does not, and a rendering file named for a custom `pageExtensions` (`page.page.tsx`), which it
|
|
27
|
+
* does not find. It parses each file once and settles what every file reaches in one pass over the
|
|
27
28
|
* graph, so its cost grows with the app's size, not with its pages times its modules.
|
|
28
29
|
*/
|
|
29
30
|
|
|
@@ -100,7 +101,10 @@ function typeOnly(node: ts.ImportDeclaration | ts.ExportDeclaration): boolean {
|
|
|
100
101
|
)
|
|
101
102
|
}
|
|
102
103
|
|
|
103
|
-
/**
|
|
104
|
+
/**
|
|
105
|
+
* The specifiers `file` loads at run time: value import and export declarations, `import x = require()` (not
|
|
106
|
+
* `import type`), and `import()` or `require()` of a literal.
|
|
107
|
+
*/
|
|
104
108
|
function loadedSpecifiers(file: string): string[] {
|
|
105
109
|
const source = ts.createSourceFile(file, readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true)
|
|
106
110
|
const found: string[] = []
|
|
@@ -108,9 +112,17 @@ function loadedSpecifiers(file: string): string[] {
|
|
|
108
112
|
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
|
|
109
113
|
const specifier = node.moduleSpecifier
|
|
110
114
|
if (specifier && ts.isStringLiteral(specifier) && !typeOnly(node)) found.push(specifier.text)
|
|
115
|
+
} else if (
|
|
116
|
+
ts.isImportEqualsDeclaration(node) &&
|
|
117
|
+
!node.isTypeOnly &&
|
|
118
|
+
ts.isExternalModuleReference(node.moduleReference) &&
|
|
119
|
+
ts.isStringLiteralLike(node.moduleReference.expression)
|
|
120
|
+
) {
|
|
121
|
+
found.push(node.moduleReference.expression.text)
|
|
111
122
|
} else if (
|
|
112
123
|
ts.isCallExpression(node) &&
|
|
113
|
-
node.expression.kind === ts.SyntaxKind.ImportKeyword
|
|
124
|
+
(node.expression.kind === ts.SyntaxKind.ImportKeyword ||
|
|
125
|
+
(ts.isIdentifier(node.expression) && node.expression.text === 'require')) &&
|
|
114
126
|
node.arguments[0] &&
|
|
115
127
|
ts.isStringLiteralLike(node.arguments[0])
|
|
116
128
|
) {
|
|
@@ -9,10 +9,14 @@ import type { CheckContext, CheckImpl } from './types'
|
|
|
9
9
|
* `route.ts` from anywhere there), and judges each exported mutating handler, read with the TypeScript parser, never
|
|
10
10
|
* the file's text: the handler must reach `authorize` on its own path, or its route must have a justified exemption
|
|
11
11
|
* (the framework's, ../../authz/route-policy.ts, or the app's own `authz-exemptions.mjs`) and the handler must pass
|
|
12
|
-
* that exemption's gate.
|
|
12
|
+
* that exemption's gate. The same scan judges every Server Action under the app's `src` (each export of a
|
|
13
|
+
* `'use server'` file, each function whose body opens with `'use server'`) like a POST, with no exemption, and fails
|
|
14
|
+
* what Next serves and it cannot read: a `pages/api` or root-level `app/` directory, a `route.<ext>` it does not
|
|
15
|
+
* parse, a `'use server'` file outside `src`.
|
|
13
16
|
*
|
|
14
17
|
* The test names are fixed (doctor computes them), so every failing handler is reported in ONE assertion, one line
|
|
15
|
-
* each
|
|
18
|
+
* each: `<app>: <rel> <VERB> …why` for a handler, `<app>: <rel> Server Action \`name\` …why` for an action, and a
|
|
19
|
+
* plain sentence for a directory or file the scan cannot read.
|
|
16
20
|
*/
|
|
17
21
|
const scans = new Map<string, Promise<AppScan>>()
|
|
18
22
|
const scanOf = ({ appDir }: CheckContext): Promise<AppScan> => {
|
|
@@ -53,7 +53,7 @@ function mockServerOnlyWorld(): void {
|
|
|
53
53
|
vi.doMock('../../adapters/index', async () => ({
|
|
54
54
|
[MOCK_MARK]: true,
|
|
55
55
|
isSimulated: true,
|
|
56
|
-
auth: (await import('../../adapters/fake/auth')).
|
|
56
|
+
auth: (await import('../../adapters/fake/simulated-auth')).simulatedAuth(),
|
|
57
57
|
db: (await import('../../adapters/fake/db')).fakeDb,
|
|
58
58
|
email: (await import('../../adapters/fake/email')).fakeEmail,
|
|
59
59
|
analytics: (await import('../../adapters/fake/analytics')).fakeAnalytics,
|
|
@@ -262,7 +262,7 @@ async function replayEveryPreset(own: ScratchWorld): Promise<void> {
|
|
|
262
262
|
const { presets } = await registries()
|
|
263
263
|
const { expandPreset } = await import('../../core/presets')
|
|
264
264
|
const { applyDemoPreset } = await import('../../server-lib/demo-presets')
|
|
265
|
-
const
|
|
265
|
+
const auth = (await import('../../adapters/fake/simulated-auth')).simulatedAuth()
|
|
266
266
|
const { readActorHolds } = await import('../../adapters/fake/simulator')
|
|
267
267
|
for (const preset of presets) {
|
|
268
268
|
// What replays is the EXPANDED preset (`extends` chain flattened): a child's inherited invites
|
|
@@ -282,14 +282,14 @@ async function replayEveryPreset(own: ScratchWorld): Promise<void> {
|
|
|
282
282
|
expect(readActorHolds(), `${preset.id}: actor holds`).toEqual(expectedHolds)
|
|
283
283
|
for (const operation of expanded.operations) {
|
|
284
284
|
if (operation.op !== 'invite') continue
|
|
285
|
-
const members = await
|
|
285
|
+
const members = await auth.listMembers(operation.org)
|
|
286
286
|
expect(
|
|
287
287
|
members.some((member) => member.status === 'invited' && member.email === operation.email),
|
|
288
288
|
`${preset.id}: invite to ${operation.email}`,
|
|
289
289
|
).toBe(true)
|
|
290
290
|
}
|
|
291
291
|
if (expanded.viewpoint !== undefined) {
|
|
292
|
-
expect((await
|
|
292
|
+
expect((await auth.getCurrentUser())?.id, preset.id).toBe(expanded.viewpoint)
|
|
293
293
|
}
|
|
294
294
|
}
|
|
295
295
|
}
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isDateElement,
|
|
3
|
+
isLiteralElement,
|
|
4
|
+
isNumberElement,
|
|
5
|
+
isPluralElement,
|
|
6
|
+
isPoundElement,
|
|
7
|
+
isSelectElement,
|
|
8
|
+
isTagElement,
|
|
9
|
+
isTimeElement,
|
|
10
|
+
type MessageFormatElement,
|
|
11
|
+
parse,
|
|
12
|
+
} from '@formatjs/icu-messageformat-parser'
|
|
1
13
|
import { expect } from 'vitest'
|
|
2
14
|
import enFramework from '../../i18n/messages/en.json'
|
|
3
15
|
import esFramework from '../../i18n/messages/es.json'
|
|
@@ -46,22 +58,104 @@ function flatten(tree: MessageTree, prefix = ''): Map<string, string> {
|
|
|
46
58
|
return out
|
|
47
59
|
}
|
|
48
60
|
|
|
49
|
-
|
|
61
|
+
/**
|
|
62
|
+
* What a translation of `message` must keep, as sorted strings: each argument with its type (`count:plural`,
|
|
63
|
+
* `name:argument`, `d:date`), including arguments nested inside a plural or select branch, and each tag (`<b>`).
|
|
64
|
+
* Branch text is text, a quoted `'{literal}'` is text, and branch keys are not compared (Spanish and English need
|
|
65
|
+
* different plural categories). It is a set: an argument used in one branch of one side and in every branch of the
|
|
66
|
+
* other matches. Throws when `message` is not ICU.
|
|
67
|
+
*/
|
|
68
|
+
export function icuSignature(message: string): string[] {
|
|
69
|
+
const found = new Set<string>()
|
|
70
|
+
const visit = (elements: MessageFormatElement[]): void => {
|
|
71
|
+
for (const element of elements) {
|
|
72
|
+
if (isLiteralElement(element) || isPoundElement(element)) continue
|
|
73
|
+
if (isTagElement(element)) {
|
|
74
|
+
found.add(`<${element.value}>`)
|
|
75
|
+
visit(element.children)
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
const type = isPluralElement(element)
|
|
79
|
+
? element.pluralType === 'ordinal'
|
|
80
|
+
? 'selectordinal'
|
|
81
|
+
: 'plural'
|
|
82
|
+
: isSelectElement(element)
|
|
83
|
+
? 'select'
|
|
84
|
+
: isNumberElement(element)
|
|
85
|
+
? 'number'
|
|
86
|
+
: isDateElement(element)
|
|
87
|
+
? 'date'
|
|
88
|
+
: isTimeElement(element)
|
|
89
|
+
? 'time'
|
|
90
|
+
: 'argument'
|
|
91
|
+
found.add(`${element.value}:${type}`)
|
|
92
|
+
if (isPluralElement(element) || isSelectElement(element)) {
|
|
93
|
+
for (const option of Object.values(element.options)) visit(option.value)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
visit(parse(message))
|
|
98
|
+
return [...found].sort()
|
|
99
|
+
}
|
|
50
100
|
|
|
51
|
-
function
|
|
101
|
+
function signatureOrError(message: string): string[] | string {
|
|
102
|
+
try {
|
|
103
|
+
return icuSignature(message)
|
|
104
|
+
} catch (error) {
|
|
105
|
+
return `does not parse as ICU (${(error as Error).message})`
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The `{name}` placeholder names in a value that is not ICU, sorted: what a raw pair (a value read with next-intl's
|
|
111
|
+
* `t.raw()`, such as markup, JSON or a template) must still agree on.
|
|
112
|
+
*/
|
|
113
|
+
const rawPlaceholderNames = (value: string) => [...new Set([...value.matchAll(/\{(\w+)/g)].map((m) => m[1]!))].sort()
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Every way `es` is not a faithful counterpart of `en`: a key on one side only, or a message whose signature differs.
|
|
117
|
+
* A pair that parses as ICU on NEITHER side is a raw value (`t.raw()`), compared by its `{name}` placeholder names
|
|
118
|
+
* only; a pair that parses on exactly one side fails, naming the side that does not.
|
|
119
|
+
*/
|
|
120
|
+
export function parityProblems(en: MessageTree, es: MessageTree): string[] {
|
|
52
121
|
const enFlat = flatten(en)
|
|
53
122
|
const esFlat = flatten(es)
|
|
54
|
-
|
|
123
|
+
const problems: string[] = []
|
|
124
|
+
for (const key of enFlat.keys()) if (!esFlat.has(key)) problems.push(`${key}: missing from es`)
|
|
125
|
+
for (const key of esFlat.keys()) if (!enFlat.has(key)) problems.push(`${key}: missing from en`)
|
|
55
126
|
for (const [key, enMsg] of enFlat) {
|
|
56
127
|
const esMsg = esFlat.get(key)
|
|
57
|
-
if (esMsg
|
|
128
|
+
if (esMsg === undefined) continue
|
|
129
|
+
const enSig = signatureOrError(enMsg)
|
|
130
|
+
const esSig = signatureOrError(esMsg)
|
|
131
|
+
if (typeof enSig === 'string' && typeof esSig === 'string') {
|
|
132
|
+
const enRaw = rawPlaceholderNames(enMsg)
|
|
133
|
+
const esRaw = rawPlaceholderNames(esMsg)
|
|
134
|
+
if (enRaw.join() !== esRaw.join()) {
|
|
135
|
+
problems.push(
|
|
136
|
+
`${key}: a raw value (ICU on neither side) with en {${enRaw.join(', ')}}, es {${esRaw.join(', ')}}`,
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
} else if (typeof enSig === 'string') {
|
|
140
|
+
problems.push(`${key}: en ${enSig}, while es parses`)
|
|
141
|
+
} else if (typeof esSig === 'string') {
|
|
142
|
+
problems.push(`${key}: es ${esSig}, while en parses`)
|
|
143
|
+
} else if (enSig.join() !== esSig.join()) {
|
|
144
|
+
problems.push(`${key}: en has [${enSig.join(', ')}], es has [${esSig.join(', ')}]`)
|
|
145
|
+
}
|
|
58
146
|
}
|
|
147
|
+
return problems
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function expectParity(en: MessageTree, es: MessageTree): void {
|
|
151
|
+
expect(parityProblems(en, es)).toEqual([])
|
|
59
152
|
}
|
|
60
153
|
|
|
61
154
|
/**
|
|
62
155
|
* `i18n-parity`: en/es parity over the MERGED catalog (the framework half + the app half, which arrives through the
|
|
63
156
|
* ADR-0012 seam). The catalogs are physically split, but what ships to the user is the merge — so that is what must
|
|
64
|
-
* stay key- and placeholder-identical
|
|
157
|
+
* stay key- and placeholder-identical: the same arguments, each of the same type (inside plural and select
|
|
158
|
+
* branches too), and the same tags (`icuSignature`). A raw value, ICU on neither side, keeps its `{name}` placeholders. The framework half is checked on its own too, so the package carries a
|
|
65
159
|
* truthful guarantee independent of whatever app consumes it.
|
|
66
160
|
*/
|
|
67
161
|
export const i18nParity: CheckImpl = {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { existsSync, realpathSync } from 'node:fs'
|
|
1
2
|
import path from 'node:path'
|
|
3
|
+
import { expect } from 'vitest'
|
|
2
4
|
import { realAuthProvider } from './auth-provider'
|
|
3
5
|
import { authzMutations } from './authz'
|
|
4
6
|
import { appRolesCheck, migrationOrder, rlsPglite } from './db'
|
|
@@ -46,16 +48,74 @@ export interface ConformanceOptions {
|
|
|
46
48
|
optOut?: Readonly<Record<string, string>>
|
|
47
49
|
}
|
|
48
50
|
|
|
51
|
+
/** The nearest directory at or above `file` that holds a package.json, or null. */
|
|
52
|
+
function packageDirOf(file: string): string | null {
|
|
53
|
+
for (let dir = path.dirname(file); ; dir = path.dirname(dir)) {
|
|
54
|
+
if (existsSync(path.join(dir, 'package.json'))) return dir
|
|
55
|
+
if (path.dirname(dir) === dir) return null
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const realOrNull = (dir: string) => {
|
|
60
|
+
try {
|
|
61
|
+
// The native call: on a case-insensitive file system it answers the name as stored, where the JS one keeps
|
|
62
|
+
// the caller's casing, so `/apps/Starter` and `/apps/starter` would compare unequal.
|
|
63
|
+
return realpathSync.native(dir)
|
|
64
|
+
} catch {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Why `appDir` is not the app whose suite file `testPath` is, or null when it is. The app is the nearest directory at
|
|
71
|
+
* or above the test file that holds a package.json; both sides are compared by native real path, so `..`, symbolic
|
|
72
|
+
* links and (on a case-insensitive file system) letter case do not matter. A suite file in one app pointed at another would otherwise run every file-reading check over the
|
|
73
|
+
* other app's files and pass.
|
|
74
|
+
*/
|
|
75
|
+
export function appDirMismatch(appDir: string, testPath: string): string | null {
|
|
76
|
+
const own = packageDirOf(realOrNull(testPath) ?? testPath)
|
|
77
|
+
const given = realOrNull(path.resolve(appDir))
|
|
78
|
+
const ownReal = own === null ? null : realOrNull(own)
|
|
79
|
+
if (given !== null && ownReal !== null && given === ownReal) return null
|
|
80
|
+
return (
|
|
81
|
+
`conformanceSuite: appDir is ${path.resolve(appDir)}, but the suite file ${testPath} belongs to ` +
|
|
82
|
+
`${own ?? 'no package (no package.json above it)'}; appDir must be the directory of the app that runs it`
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
49
86
|
/** Registers the conformance suite in the calling test file. Call it once, at the file's top level. */
|
|
50
87
|
export function conformanceSuite(options: ConformanceOptions): void {
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
|
|
88
|
+
// Only the two published options are read: anything else an app passes is ignored, so nothing it writes can turn
|
|
89
|
+
// the appDir check below off.
|
|
90
|
+
registerConformanceSuite({ appDir: options.appDir, ...(options.optOut ? { optOut: options.optOut } : {}) })
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* SSSB's own entry, not a published subpath (`@sssb/kit/testing` exports `conformanceSuite` only): the suite with
|
|
95
|
+
* `appDirOutsideCaller`, the reason `appDir` is not the calling test file's package. The kit runs the suite against
|
|
96
|
+
* its fixture, `packages/kit/test-fixture`, from a test file in `packages/kit`; an app never has a reason to.
|
|
97
|
+
*/
|
|
98
|
+
export function registerConformanceSuite(options: ConformanceOptions & { appDirOutsideCaller?: string }): void {
|
|
54
99
|
const optOut = options.optOut ?? {}
|
|
55
100
|
checkOptOut('conformanceSuite', 'unit', optOut)
|
|
56
101
|
if (typeof options.appDir !== 'string' || options.appDir === '') {
|
|
57
102
|
throw new Error('conformanceSuite: appDir must name the app directory')
|
|
58
103
|
}
|
|
104
|
+
// Refused while the file is collected, before anything is registered.
|
|
105
|
+
const outside = options.appDirOutsideCaller
|
|
106
|
+
if (outside !== undefined) {
|
|
107
|
+
if (typeof outside !== 'string' || !outside.trim()) {
|
|
108
|
+
throw new Error('conformanceSuite: appDirOutsideCaller needs a reason')
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
const testPath = expect.getState().testPath
|
|
112
|
+
if (!testPath) throw new Error('conformanceSuite: call it at the top level of a Vitest test file')
|
|
113
|
+
const mismatch = appDirMismatch(options.appDir, testPath)
|
|
114
|
+
if (mismatch) throw new Error(mismatch)
|
|
115
|
+
}
|
|
116
|
+
// Before the checks, so the scratch directories' cleanup is this file's own, at its top level, and runs after the
|
|
117
|
+
// checks' teardown, even under `isolate: false`, where the helper loads once per worker (../tmp-dir.ts).
|
|
118
|
+
registerTestTmpDirCleanup()
|
|
59
119
|
const context: CheckContext = { appDir: path.resolve(options.appDir) }
|
|
60
120
|
registerChecks({ caller: 'conformanceSuite', runner: 'unit', implementations: IMPLEMENTATIONS, optOut, context })
|
|
61
121
|
}
|