@sssb/kit 0.3.0 → 0.5.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.
Files changed (67) hide show
  1. package/dist/release.json +1 -1
  2. package/package.json +3 -1
  3. package/src/adapters/email-message.ts +30 -0
  4. package/src/adapters/fake/analytics.ts +1 -1
  5. package/src/adapters/fake/auth.ts +31 -4
  6. package/src/adapters/fake/data-dir.ts +32 -2
  7. package/src/adapters/fake/db.ts +4 -2
  8. package/src/adapters/fake/email.ts +2 -0
  9. package/src/adapters/real/absent.ts +13 -0
  10. package/src/adapters/real/accept-invite-form.tsx +86 -21
  11. package/src/adapters/real/analytics.ts +30 -3
  12. package/src/adapters/real/auth.ts +82 -20
  13. package/src/adapters/real/contract-postgres.ts +15 -11
  14. package/src/adapters/real/db-port.ts +3 -2
  15. package/src/adapters/real/email.ts +2 -0
  16. package/src/adapters/real/index.ts +49 -12
  17. package/src/adapters/real/jobs-in-process.ts +32 -14
  18. package/src/adapters/real/jobs.ts +6 -3
  19. package/src/adapters/real/migrate-handler.ts +3 -1
  20. package/src/adapters/real/no-analytics.ts +17 -0
  21. package/src/adapters/real/optional-ports.ts +35 -1
  22. package/src/adapters/real/rds-data-codec.ts +88 -4
  23. package/src/adapters/real/rds-data-emulator.ts +12 -4
  24. package/src/adapters/real/s3-storage.ts +7 -1
  25. package/src/adapters/real/sign-in-form.tsx +29 -13
  26. package/src/adapters/real/storage-settings.ts +37 -0
  27. package/src/adapters/real/storage.ts +65 -0
  28. package/src/authz/app-exemptions.ts +79 -9
  29. package/src/authz/route-scan.ts +626 -55
  30. package/src/components/auth/accept-invite-screen.tsx +5 -1
  31. package/src/components/auth/people-picker.tsx +20 -2
  32. package/src/components/profile-screen.tsx +27 -2
  33. package/src/core/inbound-email.ts +41 -6
  34. package/src/cutover/rows.json +13 -1
  35. package/src/db/audit.ts +2 -2
  36. package/src/db/family-suite.ts +42 -0
  37. package/src/db/family.ts +91 -0
  38. package/src/db/jobs.ts +19 -9
  39. package/src/db/keyset.ts +19 -30
  40. package/src/db/migrations/0015_app_user_grant.ts +1 -1
  41. package/src/db/notifications.ts +2 -2
  42. package/src/db/rls-coverage.ts +51 -17
  43. package/src/db/rls-proof-runner.ts +3 -2
  44. package/src/db/schedules.ts +11 -4
  45. package/src/db/webhooks.ts +4 -4
  46. package/src/demo-static/actor-drivers.ts +8 -2
  47. package/src/demo-static/email-html.ts +9 -0
  48. package/src/demo-static/tour/use-tours.tsx +8 -2
  49. package/src/demo-static/world.ts +76 -37
  50. package/src/email/send.ts +6 -2
  51. package/src/guide.md +26 -7
  52. package/src/i18n/messages/en.json +3 -0
  53. package/src/i18n/messages/es.json +3 -0
  54. package/src/i18n/messages.ts +3 -2
  55. package/src/ports/auth.ts +21 -2
  56. package/src/ports/db.ts +19 -0
  57. package/src/server-lib/invite.ts +34 -26
  58. package/src/server-lib/scheduled-tick.ts +5 -1
  59. package/src/server-lib/webhook-dispatch.ts +3 -0
  60. package/src/service-auth/mailgun.ts +19 -4
  61. package/src/testing/checks.json +9 -0
  62. package/src/testing/conformance/auth-provider.ts +308 -0
  63. package/src/testing/conformance/authz.ts +6 -2
  64. package/src/testing/conformance/db.ts +6 -5
  65. package/src/testing/conformance/i18n.ts +99 -5
  66. package/src/testing/conformance/suite.ts +65 -3
  67. package/src/testing/contract.ts +6 -4
@@ -1,4 +1,7 @@
1
+ import { existsSync, realpathSync } from 'node:fs'
1
2
  import path from 'node:path'
3
+ import { expect } from 'vitest'
4
+ import { realAuthProvider } from './auth-provider'
2
5
  import { authzMutations } from './authz'
3
6
  import { appRolesCheck, migrationOrder, rlsPglite } from './db'
4
7
  import { demoPresets } from './demo-presets'
@@ -28,6 +31,7 @@ import { registerTestTmpDirCleanup } from '../tmp-dir'
28
31
  /** The unit checks' implementations, by id. The contract checks are `@sssb/kit/testing/contract`'s. */
29
32
  const IMPLEMENTATIONS: Readonly<Record<string, CheckImpl>> = {
30
33
  'authz-mutations': authzMutations,
34
+ 'real-auth-provider': realAuthProvider,
31
35
  'i18n-partition': i18nPartition,
32
36
  'i18n-parity': i18nParity,
33
37
  'rls-pglite': rlsPglite,
@@ -44,16 +48,74 @@ export interface ConformanceOptions {
44
48
  optOut?: Readonly<Record<string, string>>
45
49
  }
46
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
+
47
86
  /** Registers the conformance suite in the calling test file. Call it once, at the file's top level. */
48
87
  export function conformanceSuite(options: ConformanceOptions): void {
49
- // First, so the scratch directories' cleanup is this file's own, at its top level, and runs after the checks'
50
- // teardown, even under `isolate: false`, where the helper loads once per worker (../tmp-dir.ts).
51
- registerTestTmpDirCleanup()
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 {
52
99
  const optOut = options.optOut ?? {}
53
100
  checkOptOut('conformanceSuite', 'unit', optOut)
54
101
  if (typeof options.appDir !== 'string' || options.appDir === '') {
55
102
  throw new Error('conformanceSuite: appDir must name the app directory')
56
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()
57
119
  const context: CheckContext = { appDir: path.resolve(options.appDir) }
58
120
  registerChecks({ caller: 'conformanceSuite', runner: 'unit', implementations: IMPLEMENTATIONS, optOut, context })
59
121
  }
@@ -1,6 +1,7 @@
1
1
  import { sql } from 'kysely'
2
2
  import { afterAll, beforeAll, expect } from 'vitest'
3
3
  import { startContractServer, type ContractServer } from '../adapters/real/contract-postgres'
4
+ import { familySuite } from '../db/family-suite'
4
5
  import { checkOptOut, registerChecks, runsAny } from './conformance/register'
5
6
  import type { CheckImpl } from './conformance/types'
6
7
  import { registerTestTmpDirCleanup } from './tmp-dir'
@@ -77,10 +78,11 @@ interface ContractContext {
77
78
  database(): string
78
79
  }
79
80
 
80
- // Everything that composes the framework's migrations with the app's reaches the seam, so it is imported when a test
81
- // runs, not when the file is collected: `contractDatabaseName` stays importable with no seam at all.
82
- const migrate = () => import('../db/migrate')
83
- const proofs = () => import('../db/rls-proof-runner')
81
+ // Everything that composes the framework's migrations with the app's reaches the seam, so the database family hands
82
+ // it over as loaders (../db/family-suite.ts), run when a test runs, not when the file is collected: `contractDatabaseName`
83
+ // stays importable with no seam at all.
84
+ const migrate = () => familySuite.migrator()
85
+ const proofs = () => familySuite.proofs()
84
86
  const dataApi = () => import('../adapters/real/contract-data-api')
85
87
 
86
88
  const TIMEOUT_MS = 120_000