@sssb/kit 0.0.0-stage → 0.2.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 (214) hide show
  1. package/LICENSE +21 -0
  2. package/dist/core/webhook-hmac.d.ts +69 -0
  3. package/dist/release.json +1 -0
  4. package/dist/testing/e2e.d.ts +15 -0
  5. package/dist/testing/e2e.js +195 -0
  6. package/package.json +209 -3
  7. package/src/adapters/README.md +6 -0
  8. package/src/adapters/dev-server.ts +21 -0
  9. package/src/adapters/fake/agreements.ts +81 -0
  10. package/src/adapters/fake/analytics.ts +93 -0
  11. package/src/adapters/fake/atomic-write.ts +161 -0
  12. package/src/adapters/fake/audit.ts +58 -0
  13. package/src/adapters/fake/auth.ts +312 -0
  14. package/src/adapters/fake/clock.ts +54 -0
  15. package/src/adapters/fake/data-dir.ts +27 -0
  16. package/src/adapters/fake/db.ts +88 -0
  17. package/src/adapters/fake/email.ts +46 -0
  18. package/src/adapters/fake/jobs.ts +270 -0
  19. package/src/adapters/fake/llm.ts +230 -0
  20. package/src/adapters/fake/pglite-dialect.ts +124 -0
  21. package/src/adapters/fake/service-auth.ts +145 -0
  22. package/src/adapters/fake/simulator-admin.ts +203 -0
  23. package/src/adapters/fake/simulator.ts +94 -0
  24. package/src/adapters/fake/sms.ts +43 -0
  25. package/src/adapters/fake/storage.ts +288 -0
  26. package/src/adapters/fake/webhooks.ts +98 -0
  27. package/src/adapters/index.ts +115 -0
  28. package/src/adapters/real/accept-invite-form.tsx +77 -0
  29. package/src/adapters/real/analytics.ts +28 -0
  30. package/src/adapters/real/auth.ts +216 -0
  31. package/src/adapters/real/contract-data-api.ts +104 -0
  32. package/src/adapters/real/contract-postgres.ts +167 -0
  33. package/src/adapters/real/database-transport.ts +34 -0
  34. package/src/adapters/real/db-data-api.ts +61 -0
  35. package/src/adapters/real/db.ts +88 -0
  36. package/src/adapters/real/email.ts +22 -0
  37. package/src/adapters/real/index.ts +89 -0
  38. package/src/adapters/real/jobs.ts +32 -0
  39. package/src/adapters/real/llm.ts +108 -0
  40. package/src/adapters/real/migrate-handler.ts +66 -0
  41. package/src/adapters/real/providers.tsx +12 -0
  42. package/src/adapters/real/rds-data-codec.ts +508 -0
  43. package/src/adapters/real/rds-data-dialect.ts +470 -0
  44. package/src/adapters/real/rds-data-emulator.ts +351 -0
  45. package/src/adapters/real/s3-storage.ts +233 -0
  46. package/src/adapters/real/sign-in-form.tsx +59 -0
  47. package/src/adapters/real/storage.ts +10 -0
  48. package/src/adapters/sentry/baseline.ts +90 -0
  49. package/src/adapters/sentry/client.ts +17 -0
  50. package/src/adapters/sentry/init.ts +17 -0
  51. package/src/authz/app-exemptions.ts +226 -0
  52. package/src/authz/authorize.ts +214 -0
  53. package/src/authz/route-policy.ts +153 -0
  54. package/src/authz/route-scan.ts +1516 -0
  55. package/src/components/agreements/acceptances-section.tsx +55 -0
  56. package/src/components/agreements/agreement-advisory-banner.tsx +33 -0
  57. package/src/components/agreements/agreement-body.tsx +34 -0
  58. package/src/components/agreements/agreement-gate.tsx +45 -0
  59. package/src/components/app-header.tsx +45 -0
  60. package/src/components/auth/accept-invite-screen.tsx +126 -0
  61. package/src/components/auth/auth-icons.tsx +68 -0
  62. package/src/components/auth/org-switcher.tsx +76 -0
  63. package/src/components/auth/people-picker.tsx +50 -0
  64. package/src/components/auth/preview-row.tsx +64 -0
  65. package/src/components/auth/sign-in-screen.tsx +24 -0
  66. package/src/components/auth/user-menu.tsx +64 -0
  67. package/src/components/demo-badge.tsx +20 -0
  68. package/src/components/demo-banner.tsx +14 -0
  69. package/src/components/gates/gate-interstitial.tsx +23 -0
  70. package/src/components/job-timeline.tsx +64 -0
  71. package/src/components/notifications/notification-bell.tsx +120 -0
  72. package/src/components/notifications/notification-prefs-section.tsx +112 -0
  73. package/src/components/org-screen.tsx +197 -0
  74. package/src/components/profile-screen.tsx +100 -0
  75. package/src/components/simulator/actor-runtime.ts +180 -0
  76. package/src/components/simulator/actor-shell.tsx +275 -0
  77. package/src/components/simulator/actors-app.tsx +119 -0
  78. package/src/components/simulator/errors-app.tsx +84 -0
  79. package/src/components/simulator/events-app.tsx +210 -0
  80. package/src/components/simulator/format-when.ts +20 -0
  81. package/src/components/simulator/hooks-app.tsx +319 -0
  82. package/src/components/simulator/inbound-app.tsx +210 -0
  83. package/src/components/simulator/jobs-app.tsx +471 -0
  84. package/src/components/simulator/mail-app.tsx +290 -0
  85. package/src/components/simulator/messages-app.tsx +64 -0
  86. package/src/components/simulator/people-app.tsx +140 -0
  87. package/src/components/simulator/people-dimensions.ts +16 -0
  88. package/src/components/simulator/simulator-panel.tsx +716 -0
  89. package/src/components/simulator/snapshots-app.tsx +398 -0
  90. package/src/components/simulator/tab-mount.ts +25 -0
  91. package/src/components/simulator/tours-app.tsx +105 -0
  92. package/src/components/webhook-endpoints-card.tsx +172 -0
  93. package/src/core/abilities.ts +459 -0
  94. package/src/core/agreements.ts +139 -0
  95. package/src/core/gates.ts +157 -0
  96. package/src/core/greeting.ts +11 -0
  97. package/src/core/inbound-email.ts +129 -0
  98. package/src/core/jobs.ts +51 -0
  99. package/src/core/keyset.ts +227 -0
  100. package/src/core/llm-models.ts +10 -0
  101. package/src/core/locale.ts +3 -0
  102. package/src/core/notifications.ts +182 -0
  103. package/src/core/presets.ts +456 -0
  104. package/src/core/roles.ts +47 -0
  105. package/src/core/schedules.ts +147 -0
  106. package/src/core/standard-schema.ts +56 -0
  107. package/src/core/state-machine.ts +43 -0
  108. package/src/core/webhook-events.ts +86 -0
  109. package/src/core/webhook-hmac.ts +248 -0
  110. package/src/core/webhook-signing.ts +27 -0
  111. package/src/cutover/rows.json +101 -0
  112. package/src/db/agreements.ts +176 -0
  113. package/src/db/app-roles.ts +197 -0
  114. package/src/db/audit.ts +356 -0
  115. package/src/db/inbound-email.ts +145 -0
  116. package/src/db/jobs.ts +300 -0
  117. package/src/db/jsonb.ts +18 -0
  118. package/src/db/keyset.ts +179 -0
  119. package/src/db/migrate.ts +73 -0
  120. package/src/db/migrations/0001_tenants.ts +25 -0
  121. package/src/db/migrations/0003_organizations.ts +27 -0
  122. package/src/db/migrations/0004_jobs.ts +64 -0
  123. package/src/db/migrations/0005_service_keys.ts +33 -0
  124. package/src/db/migrations/0008_audit_events.ts +51 -0
  125. package/src/db/migrations/0009_job_schedules.ts +52 -0
  126. package/src/db/migrations/0010_webhooks.ts +100 -0
  127. package/src/db/migrations/0011_notifications.ts +98 -0
  128. package/src/db/migrations/0012_inbound_email.ts +74 -0
  129. package/src/db/migrations/0013_agreements.ts +98 -0
  130. package/src/db/migrations/0014_audit_credential.ts +29 -0
  131. package/src/db/migrations/0015_app_user_grant.ts +67 -0
  132. package/src/db/migrations/0016_audit_detail.ts +26 -0
  133. package/src/db/migrations/index.ts +49 -0
  134. package/src/db/notifications.ts +185 -0
  135. package/src/db/org-lookup.ts +104 -0
  136. package/src/db/owner-pass.ts +252 -0
  137. package/src/db/rls-coverage.ts +259 -0
  138. package/src/db/rls-proof-runner.ts +94 -0
  139. package/src/db/rls-proofs.ts +1176 -0
  140. package/src/db/schedules.ts +187 -0
  141. package/src/db/schema.ts +235 -0
  142. package/src/db/seed.ts +176 -0
  143. package/src/db/service-keys.ts +50 -0
  144. package/src/db/tenant-lookup.ts +13 -0
  145. package/src/db/webhooks.ts +483 -0
  146. package/src/db/with-tenant.ts +26 -0
  147. package/src/demo-static/actor-drivers.ts +161 -0
  148. package/src/demo-static/assistant-replay.ts +61 -0
  149. package/src/demo-static/contracts.ts +282 -0
  150. package/src/demo-static/email-html.ts +58 -0
  151. package/src/demo-static/hash-route.ts +24 -0
  152. package/src/demo-static/rows.ts +105 -0
  153. package/src/demo-static/shell.tsx +440 -0
  154. package/src/demo-static/tour/contracts.ts +97 -0
  155. package/src/demo-static/tour/driver.ts +320 -0
  156. package/src/demo-static/tour/tour-overlay.tsx +226 -0
  157. package/src/demo-static/tour/use-tours.tsx +394 -0
  158. package/src/demo-static/world.ts +1479 -0
  159. package/src/email/send.ts +12 -0
  160. package/src/email/templates/digest-email.tsx +61 -0
  161. package/src/email/templates/invite-email.tsx +57 -0
  162. package/src/email/templates/notification-email.tsx +39 -0
  163. package/src/guide.md +118 -0
  164. package/src/i18n/messages/en.json +373 -0
  165. package/src/i18n/messages/es.json +373 -0
  166. package/src/i18n/messages.ts +34 -0
  167. package/src/i18n/namespaces.ts +32 -0
  168. package/src/i18n/request.ts +34 -0
  169. package/src/i18n/routing.ts +7 -0
  170. package/src/inbound-email/handlers.ts +67 -0
  171. package/src/inbound-email/intake.ts +180 -0
  172. package/src/jobs/digest-email.ts +97 -0
  173. package/src/observability/scrub.ts +42 -0
  174. package/src/ports/README.md +8 -0
  175. package/src/ports/analytics.ts +8 -0
  176. package/src/ports/auth.ts +87 -0
  177. package/src/ports/db.ts +27 -0
  178. package/src/ports/email.ts +14 -0
  179. package/src/ports/errors.ts +46 -0
  180. package/src/ports/jobs.ts +48 -0
  181. package/src/ports/llm.ts +85 -0
  182. package/src/ports/storage.ts +132 -0
  183. package/src/seed/contracts.ts +129 -0
  184. package/src/server-lib/assistant.ts +45 -0
  185. package/src/server-lib/defer.ts +53 -0
  186. package/src/server-lib/demo-presets.ts +85 -0
  187. package/src/server-lib/invite.ts +92 -0
  188. package/src/server-lib/notify-deps.ts +15 -0
  189. package/src/server-lib/notify.ts +252 -0
  190. package/src/server-lib/preset-operations.ts +121 -0
  191. package/src/server-lib/scheduled-tick.ts +38 -0
  192. package/src/server-lib/sms.ts +43 -0
  193. package/src/server-lib/webhook-dispatch.ts +75 -0
  194. package/src/service-auth/mailgun.ts +70 -0
  195. package/src/service-auth/verify.ts +73 -0
  196. package/src/service-auth/webhook.ts +60 -0
  197. package/src/testing/checks.json +103 -0
  198. package/src/testing/conformance/authz.ts +46 -0
  199. package/src/testing/conformance/db.ts +258 -0
  200. package/src/testing/conformance/demo-presets.ts +295 -0
  201. package/src/testing/conformance/i18n.ts +80 -0
  202. package/src/testing/conformance/register.ts +200 -0
  203. package/src/testing/conformance/seam-map.ts +205 -0
  204. package/src/testing/conformance/suite.ts +59 -0
  205. package/src/testing/conformance/types.ts +18 -0
  206. package/src/testing/contract.ts +200 -0
  207. package/src/testing/e2e.ts +15 -0
  208. package/src/testing/index.ts +6 -0
  209. package/src/testing/seam-specifiers.json +22 -0
  210. package/src/testing/tmp-dir-marker.d.mts +4 -0
  211. package/src/testing/tmp-dir-marker.mjs +5 -0
  212. package/src/testing/tmp-dir.ts +182 -0
  213. package/src/theme.ts +28 -0
  214. package/README.md +0 -3
@@ -0,0 +1,182 @@
1
+ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
2
+ import { tmpdir } from 'node:os'
3
+ import path from 'node:path'
4
+ import { afterAll, expect } from 'vitest'
5
+ import { TMP_DIR_MARKER_SUFFIX } from './tmp-dir-marker.mjs'
6
+
7
+ /**
8
+ * THE FRAMEWORK'S OWN COPY of the scratch-directory helper, for @sssb/kit's tests (the shipped conformance and contract
9
+ * suites among them). Everyone else imports @sssb/tools' (`@sssb/tools/testing/tmp-dir`); the framework keeps a copy
10
+ * because the framework depends on no other SSSB package (ADR-0014, rule 4). Both write the same marker, which
11
+ * tests/tooling/tmp-dir.test.ts holds equal, so `sssb check-tmpdir-leak` sees either one's leaks. This one is not in
12
+ * the package's `exports`: an adopter reaches it only through `conformanceSuite` and `contractSuite`.
13
+ *
14
+ * THE ONLY WAY A TEST MAKES A SCRATCH DIRECTORY. A raw `mkdtemp` in a test file leaves the directory
15
+ * behind on every run; left in place long enough that has filled a developer's temp filesystem past
16
+ * 100GB. `sssb/no-direct-mkdtemp` (@sssb/eslint-plugin) bans the raw call in test files, and
17
+ * `sssb check-tmpdir-leak` fails the run if a directory made here was not removed.
18
+ *
19
+ * Each call creates `<tmpdir>/<prefix>XXXXXX`, writes an EMPTY MARKER FILE beside it
20
+ * (`<dir>` + TMP_DIR_MARKER_SUFFIX) and queues both for removal when the test file finishes. The
21
+ * marker is a SIBLING, never inside the directory: some tests assert the exact contents of their
22
+ * directory (atomic-write.test.ts lists it), and a marker inside would be a fixture they have to know
23
+ * about. The marker is deleted only after its directory is, so a directory that cannot be removed
24
+ * keeps its marker and the leak guard still sees it.
25
+ *
26
+ * THE FILE'S CLEANUP HOOK is one `afterAll` at the file's top level, which removes every directory still queued: a
27
+ * worker runs its files one after another, so anything queued is this file's or an earlier one's. It must exist before
28
+ * the functions are called, because Vitest silently drops a hook registered at run time from inside another hook (the
29
+ * suites make their directories in `beforeAll`). Two things register it:
30
+ *
31
+ * * loading this module, which with isolation (Vitest's default, and this repository's) happens in every file;
32
+ * * `conformanceSuite` and `contractSuite`, which call `registerTestTmpDirCleanup()` first, from the top level of the
33
+ * app's file, where an app must call them. Without isolation (`isolate: false`, as an adopter's config may run
34
+ * them) this module loads once per worker and its load-time hook is the first file's alone, so this is what gives
35
+ * every later file its own.
36
+ *
37
+ * ORDERING: `afterAll` hooks run in reverse registration order, so a hook registered first runs last. Registered at
38
+ * load or at the start of a suite, before the suite's own `afterAll` (the contract server's stop, the demo-presets
39
+ * teardown), the cleanup runs after them: a database living in the directory is closed before the directory is
40
+ * deleted.
41
+ *
42
+ * A LATER FILE WITH NO HOOK (without isolation, a file that uses this helper but registers no hook, which a suite
43
+ * called at a file's top level never is) never removes its own directories: no hook registered at a call can be both
44
+ * last in the order and wide enough in scope. Its first call registers a hook that removes EARLIER files' directories,
45
+ * which have finished, so each worker's last such file's are left for `sssb check-tmpdir-leak` to report, and the
46
+ * first such call says so once per worker. `@sssb/tools`' copy works the same way
47
+ * (`.claude/future-tasks/resolved/tools-tmp-dir-without-isolation.md` says why).
48
+ *
49
+ * LIMITS, both Vitest's: a file whose tests are all skipped runs no `afterAll`, so its directories are left until the
50
+ * next file in the worker registers, and with isolation for good. And the order above holds for
51
+ * `sequence.hooks: 'stack'`, Vitest's default.
52
+ *
53
+ * packages/kit/src/testing/conformance/adopter-config.test.ts runs each case in a real Vitest.
54
+ */
55
+
56
+ /** Every directory made and not yet removed, with the test file it was made in. */
57
+ const created: { dir: string; file: string }[] = []
58
+
59
+ /** Test files with a file-level hook, by path ('' when Vitest names none). */
60
+ const hookedFiles = new Set<string>()
61
+
62
+ /** Test files without one that have a hook removing EARLIER files' directories. */
63
+ const sweepingFiles = new Set<string>()
64
+
65
+ /** Directories handed out of the file hook's reach by `keepTestTmpDir`, still to be removed by `removeTestTmpDir`. */
66
+ const kept = new Set<string>()
67
+
68
+ let warned = false
69
+
70
+ const currentFile = (): string => expect.getState().testPath ?? ''
71
+
72
+ function remove(dir: string): void {
73
+ try {
74
+ rmSync(dir, { recursive: true, force: true })
75
+ rmSync(dir + TMP_DIR_MARKER_SUFFIX, { force: true })
76
+ } catch (error) {
77
+ console.error(`tmp-dir: could not remove ${dir}`, error)
78
+ }
79
+ }
80
+
81
+ function removeQueued(which: (entry: { dir: string; file: string }) => boolean): void {
82
+ for (let i = created.length - 1; i >= 0; i--) {
83
+ const entry = created[i]
84
+ if (entry && which(entry)) {
85
+ created.splice(i, 1)
86
+ remove(entry.dir)
87
+ }
88
+ }
89
+ }
90
+
91
+ /**
92
+ * A file's first registration: whatever is still queued or registered for another file is an earlier file's, which has
93
+ * finished (a worker runs its files one after another). Its directories go now, which also covers a file whose tests
94
+ * were all skipped, since Vitest runs no `afterAll` for a skipped suite; and its registry entries go, so a watch-mode
95
+ * rerun of that file registers again.
96
+ */
97
+ function startFile(file: string): void {
98
+ removeQueued((entry) => entry.file !== file)
99
+ for (const other of [...hookedFiles, ...sweepingFiles]) {
100
+ if (other !== file) {
101
+ hookedFiles.delete(other)
102
+ sweepingFiles.delete(other)
103
+ }
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Register the current test file's cleanup hook, if it has none. `conformanceSuite` and `contractSuite` call it first,
109
+ * at the top level of the app's file; never from a hook or a test, where Vitest drops it.
110
+ */
111
+ export function registerTestTmpDirCleanup(): void {
112
+ const file = currentFile()
113
+ if (hookedFiles.has(file)) return
114
+ startFile(file)
115
+ hookedFiles.add(file)
116
+ afterAll(() => {
117
+ // A watch-mode rerun of this file, in this worker, registers again.
118
+ hookedFiles.delete(file)
119
+ removeQueued(() => true)
120
+ })
121
+ }
122
+
123
+ registerTestTmpDirCleanup()
124
+
125
+ /**
126
+ * The file the next directory belongs to. In a file without a file-level hook, also registers that file's hook for
127
+ * earlier files' directories, on its first call, and says once per worker that the file has none.
128
+ */
129
+ function claim(): string {
130
+ const file = currentFile()
131
+ if (hookedFiles.has(file)) return file
132
+ if (!warned) {
133
+ warned = true
134
+ console.error(
135
+ `tmp-dir: ${file || 'a test file'} has no cleanup hook of its own: with Vitest's isolate: false, call ` +
136
+ "conformanceSuite or contractSuite at the file's top level, which registers one. Without it each " +
137
+ "file's scratch directories are removed only after the next file, and the last file's are left behind.",
138
+ )
139
+ }
140
+ if (!sweepingFiles.has(file)) {
141
+ startFile(file)
142
+ sweepingFiles.add(file)
143
+ afterAll(() => {
144
+ // A watch-mode rerun of this file, in this worker, registers again.
145
+ sweepingFiles.delete(file)
146
+ removeQueued((entry) => entry.file !== file)
147
+ })
148
+ }
149
+ return file
150
+ }
151
+
152
+ /** Make a scratch directory that is removed when the test file finishes. Synchronous variant. */
153
+ export function makeTestTmpDir(prefix: string): string {
154
+ const file = claim()
155
+ const dir = mkdtempSync(path.join(tmpdir(), prefix))
156
+ created.push({ dir, file })
157
+ writeFileSync(dir + TMP_DIR_MARKER_SUFFIX, '')
158
+ return dir
159
+ }
160
+
161
+ /**
162
+ * Take a directory `makeTestTmpDir` made out of the file's cleanup, for work that outlives the file (the demo-presets
163
+ * replay a timed-out test left running, ./conformance/demo-presets.ts): no hook removes it, so whatever still uses it
164
+ * removes it with `removeTestTmpDir` when done, and until then `sssb check-tmpdir-leak` sees it.
165
+ */
166
+ export function keepTestTmpDir(dir: string): void {
167
+ const index = created.findIndex((entry) => entry.dir === dir)
168
+ if (index < 0) return
169
+ created.splice(index, 1)
170
+ kept.add(dir)
171
+ }
172
+
173
+ /**
174
+ * Remove a directory `makeTestTmpDir` made, and its marker, now rather than when the file finishes. Does nothing for a
175
+ * path it did not make, or has already removed: anything else is not a scratch directory.
176
+ */
177
+ export function removeTestTmpDir(dir: string): void {
178
+ const index = created.findIndex((entry) => entry.dir === dir)
179
+ if (index >= 0) created.splice(index, 1)
180
+ else if (!kept.delete(dir)) return
181
+ remove(dir)
182
+ }
package/src/theme.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { createTheme, type MantineThemeOverride } from '@mantine/core'
2
+ import { findTenant, tenants } from '@sssb/app-config/seed'
3
+
4
+ /**
5
+ * Per-tenant theming seam (ADR-0005). Until real tenant resolution arrives with the tenancy
6
+ * phase, the active tenant comes from seed records; each seeded tenant has a distinct theme so
7
+ * the seam stays exercised.
8
+ */
9
+ /**
10
+ * The tenant to theme by when nobody is signed in. Read from the app's OWN seed rather than named
11
+ * here: a framework constant spelling one app's tenant slug is a weld, and the second app had to work
12
+ * around it. First seeded tenant wins, matching getTenantTheme's own fallback below.
13
+ */
14
+ export const DEFAULT_TENANT_SLUG = tenants[0]?.slug ?? ''
15
+
16
+ export function getTenantTheme(tenantSlug: string): MantineThemeOverride {
17
+ const tenant = findTenant(tenantSlug) ?? tenants[0]
18
+ return createTheme({
19
+ primaryColor: tenant?.themePrimaryColor ?? 'indigo',
20
+ // Shade 8 keeps white-on-primary (filled buttons, badges) above the WCAG AA 4.5:1
21
+ // contrast ratio; Mantine's default shade 6 fails for most hues.
22
+ primaryShade: 8,
23
+ // Deliberately non-default shape token so a tenant switch is visibly a THEME switch
24
+ // (buttons/cards change roundness, not just hue) and Mantine defaults can't mask a
25
+ // broken seam.
26
+ defaultRadius: tenant?.themeRadius ?? 'md',
27
+ })
28
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.