@pikku/inspector 0.12.75 → 0.12.81

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 (219) hide show
  1. package/CHANGELOG.md +330 -0
  2. package/dist/add/add-analytics.d.ts +9 -0
  3. package/dist/add/add-analytics.js +178 -0
  4. package/dist/add/add-auth.js +0 -54
  5. package/dist/add/add-feature-flag.d.ts +2 -0
  6. package/dist/add/add-feature-flag.js +162 -0
  7. package/dist/add/add-wire-addon.js +1 -0
  8. package/dist/add/add-wire-remote-addon.js +1 -0
  9. package/dist/index.d.ts +3 -1
  10. package/dist/index.js +3 -1
  11. package/dist/inspector.js +4 -0
  12. package/dist/surface/collect-surface.d.ts +58 -0
  13. package/dist/surface/collect-surface.js +613 -0
  14. package/dist/types.d.ts +20 -11
  15. package/dist/utils/filter-inspector-state.js +14 -0
  16. package/dist/utils/post-process.js +15 -0
  17. package/dist/utils/read-module-specifiers.d.ts +8 -0
  18. package/dist/utils/read-module-specifiers.js +23 -0
  19. package/dist/utils/read-tsconfig-out-dir.d.ts +5 -0
  20. package/dist/utils/read-tsconfig-out-dir.js +12 -0
  21. package/dist/utils/resolve-deploy-target.d.ts +7 -0
  22. package/dist/utils/resolve-deploy-target.js +15 -7
  23. package/dist/utils/serialize-inspector-state.d.ts +5 -0
  24. package/dist/utils/serialize-inspector-state.js +8 -0
  25. package/dist/visit.js +4 -0
  26. package/package.json +16 -6
  27. package/run-tests.sh +21 -6
  28. package/src/add/add-addon-bans.ts +0 -85
  29. package/src/add/add-agent-scorer.test.ts +0 -197
  30. package/src/add/add-agent-scorer.ts +0 -83
  31. package/src/add/add-agent.test.ts +0 -87
  32. package/src/add/add-agent.ts +0 -638
  33. package/src/add/add-approval-description.ts +0 -76
  34. package/src/add/add-auth.test.ts +0 -791
  35. package/src/add/add-auth.ts +0 -424
  36. package/src/add/add-channel.ts +0 -778
  37. package/src/add/add-cli-renderers.test.ts +0 -74
  38. package/src/add/add-cli.ts +0 -1006
  39. package/src/add/add-credential.ts +0 -186
  40. package/src/add/add-feature.test.ts +0 -306
  41. package/src/add/add-feature.ts +0 -253
  42. package/src/add/add-file-extends-core-type.ts +0 -66
  43. package/src/add/add-file-with-config.ts +0 -45
  44. package/src/add/add-file-with-factory.test.ts +0 -134
  45. package/src/add/add-file-with-factory.ts +0 -288
  46. package/src/add/add-functions.test.ts +0 -743
  47. package/src/add/add-functions.ts +0 -1548
  48. package/src/add/add-gateway.ts +0 -143
  49. package/src/add/add-http-route-ref.test.ts +0 -149
  50. package/src/add/add-http-route.ts +0 -530
  51. package/src/add/add-http-routes.ts +0 -471
  52. package/src/add/add-keyed-wiring.ts +0 -180
  53. package/src/add/add-mcp-prompt.ts +0 -157
  54. package/src/add/add-mcp-resource.ts +0 -175
  55. package/src/add/add-middleware.test.ts +0 -196
  56. package/src/add/add-middleware.ts +0 -879
  57. package/src/add/add-permission-auth.test.ts +0 -59
  58. package/src/add/add-permission.ts +0 -311
  59. package/src/add/add-personas.test.ts +0 -406
  60. package/src/add/add-personas.ts +0 -287
  61. package/src/add/add-queue-worker.ts +0 -117
  62. package/src/add/add-rpc-invocations.test.ts +0 -287
  63. package/src/add/add-rpc-invocations.ts +0 -221
  64. package/src/add/add-scenario-step.test.ts +0 -509
  65. package/src/add/add-schedule.ts +0 -120
  66. package/src/add/add-scope.test.ts +0 -419
  67. package/src/add/add-scope.ts +0 -221
  68. package/src/add/add-secret.ts +0 -10
  69. package/src/add/add-subject-scenario-steps.test.ts +0 -161
  70. package/src/add/add-system-role.test.ts +0 -297
  71. package/src/add/add-system-role.ts +0 -162
  72. package/src/add/add-trigger.ts +0 -165
  73. package/src/add/add-variable.ts +0 -10
  74. package/src/add/add-wire-addon.test.ts +0 -185
  75. package/src/add/add-wire-addon.ts +0 -146
  76. package/src/add/add-wire-remote-addon.ts +0 -80
  77. package/src/add/add-workflow-edge-cases.test.ts +0 -201
  78. package/src/add/add-workflow-fanout.test.ts +0 -166
  79. package/src/add/add-workflow-graph-foreach.test.ts +0 -174
  80. package/src/add/add-workflow-graph-input.test.ts +0 -94
  81. package/src/add/add-workflow-graph-notes.test.ts +0 -74
  82. package/src/add/add-workflow-graph.ts +0 -644
  83. package/src/add/add-workflow-on-error.test.ts +0 -118
  84. package/src/add/add-workflow-return-switch.test.ts +0 -121
  85. package/src/add/add-workflow-silent-drops.test.ts +0 -261
  86. package/src/add/add-workflow-sleep-expression.test.ts +0 -96
  87. package/src/add/add-workflow.test.ts +0 -155
  88. package/src/add/add-workflow.ts +0 -482
  89. package/src/add/addon-bans.test.ts +0 -121
  90. package/src/add/addon-contracts.test.ts +0 -221
  91. package/src/add/allow-escape-hatches.test.ts +0 -144
  92. package/src/add/audit-meta.test.ts +0 -75
  93. package/src/add/classified-sink-check.test.ts +0 -347
  94. package/src/add/config-value-metadata.test.ts +0 -263
  95. package/src/add/dynamic-import-check.test.ts +0 -119
  96. package/src/add/expect-eventually-scenario-only.test.ts +0 -108
  97. package/src/add/function-body-span.test.ts +0 -96
  98. package/src/add/inline-wiring-function.test.ts +0 -104
  99. package/src/add/pii-check.test.ts +0 -258
  100. package/src/add/rpc-type-cast.test.ts +0 -123
  101. package/src/add/scenario-instrumentation.test.ts +0 -89
  102. package/src/add/scenario-instrumentation.ts +0 -42
  103. package/src/add/scenario-marker.test.ts +0 -89
  104. package/src/add/scenario-not-extractable.test.ts +0 -159
  105. package/src/add/scenario-services.test.ts +0 -103
  106. package/src/add/scenario-skip.test.ts +0 -83
  107. package/src/add/validate-workflow-graph-addons.test.ts +0 -102
  108. package/src/add/wire-name-literal.test.ts +0 -117
  109. package/src/error-codes-emitted.test.ts +0 -76
  110. package/src/error-codes.ts +0 -145
  111. package/src/index.ts +0 -66
  112. package/src/inspector.ts +0 -575
  113. package/src/schema-generator.ts +0 -1
  114. package/src/source-file-cache.ts +0 -73
  115. package/src/tsconfig-paths.test.ts +0 -90
  116. package/src/types-map.ts +0 -141
  117. package/src/types.ts +0 -775
  118. package/src/utils/annotate-http-route-auth.test.ts +0 -161
  119. package/src/utils/annotate-http-route-auth.ts +0 -53
  120. package/src/utils/check-classified-sinks.ts +0 -215
  121. package/src/utils/check-pii-output.ts +0 -120
  122. package/src/utils/compute-diagnostics.test.ts +0 -106
  123. package/src/utils/compute-required-schemas.ts +0 -48
  124. package/src/utils/contract-hashes.test.ts +0 -553
  125. package/src/utils/contract-hashes.ts +0 -387
  126. package/src/utils/custom-types-generator.test.ts +0 -124
  127. package/src/utils/custom-types-generator.ts +0 -150
  128. package/src/utils/detect-schema-vendor.test.ts +0 -123
  129. package/src/utils/detect-schema-vendor.ts +0 -120
  130. package/src/utils/does-type-extend-core-type.ts +0 -53
  131. package/src/utils/ensure-function-metadata.ts +0 -385
  132. package/src/utils/extract-function-name.ts +0 -612
  133. package/src/utils/extract-function-node.ts +0 -70
  134. package/src/utils/extract-node-value.test.ts +0 -117
  135. package/src/utils/extract-node-value.ts +0 -172
  136. package/src/utils/extract-secret-usage.test.ts +0 -81
  137. package/src/utils/extract-secret-usage.ts +0 -73
  138. package/src/utils/extract-services.ts +0 -70
  139. package/src/utils/extract-surface-usage.test.ts +0 -147
  140. package/src/utils/extract-surface-usage.ts +0 -121
  141. package/src/utils/filter-inspector-state.test.ts +0 -2232
  142. package/src/utils/filter-inspector-state.ts +0 -1298
  143. package/src/utils/filter-utils.test.ts +0 -843
  144. package/src/utils/filter-utils.ts +0 -161
  145. package/src/utils/find-root-dir.ts +0 -68
  146. package/src/utils/find-zod-transform.test.ts +0 -70
  147. package/src/utils/find-zod-transform.ts +0 -103
  148. package/src/utils/get-exported-variable-name.ts +0 -48
  149. package/src/utils/get-files-and-methods.ts +0 -157
  150. package/src/utils/get-property-value.test.ts +0 -141
  151. package/src/utils/get-property-value.ts +0 -331
  152. package/src/utils/hash.ts +0 -26
  153. package/src/utils/literal-properties.ts +0 -97
  154. package/src/utils/load-addon-functions-meta.test.ts +0 -157
  155. package/src/utils/load-addon-functions-meta.ts +0 -508
  156. package/src/utils/load-addon-scopes.test.ts +0 -138
  157. package/src/utils/middleware.test.ts +0 -204
  158. package/src/utils/middleware.ts +0 -303
  159. package/src/utils/nested-project-filter.test.ts +0 -53
  160. package/src/utils/nested-project-filter.ts +0 -28
  161. package/src/utils/permissions-shorthand.test.ts +0 -79
  162. package/src/utils/permissions.test.ts +0 -125
  163. package/src/utils/permissions.ts +0 -129
  164. package/src/utils/post-process.test.ts +0 -1094
  165. package/src/utils/post-process.ts +0 -1604
  166. package/src/utils/resolve-addon-package.ts +0 -106
  167. package/src/utils/resolve-core-type.test.ts +0 -142
  168. package/src/utils/resolve-core-type.ts +0 -35
  169. package/src/utils/resolve-deploy-target.test.ts +0 -135
  170. package/src/utils/resolve-deploy-target.ts +0 -65
  171. package/src/utils/resolve-function-meta.ts +0 -25
  172. package/src/utils/resolve-function-types.ts +0 -42
  173. package/src/utils/resolve-identifier.ts +0 -46
  174. package/src/utils/resolve-ref-contract.ts +0 -71
  175. package/src/utils/resolve-versions.test.ts +0 -499
  176. package/src/utils/resolve-versions.ts +0 -228
  177. package/src/utils/schema-cache.test.ts +0 -124
  178. package/src/utils/schema-generator.test.ts +0 -272
  179. package/src/utils/schema-generator.ts +0 -780
  180. package/src/utils/secret-alias-services.test.ts +0 -146
  181. package/src/utils/secret-alias-services.ts +0 -58
  182. package/src/utils/serialize-inspector-state.ts +0 -742
  183. package/src/utils/serialize-mcp-json.ts +0 -150
  184. package/src/utils/serialize-middleware-groups-meta.ts +0 -36
  185. package/src/utils/serialize-openapi-json.ts +0 -277
  186. package/src/utils/serialize-permissions-groups-meta.ts +0 -7
  187. package/src/utils/single-declaration.ts +0 -57
  188. package/src/utils/type-utils.ts +0 -145
  189. package/src/utils/validate-auth-sessionless.ts +0 -30
  190. package/src/utils/validate-exposed-functions-gated.test.ts +0 -230
  191. package/src/utils/validate-exposed-functions-gated.ts +0 -103
  192. package/src/utils/validate-tags-resolve-to-middleware.test.ts +0 -108
  193. package/src/utils/validate-tags-resolve-to-middleware.ts +0 -48
  194. package/src/utils/workflow/derive-workflow-plan.test.ts +0 -178
  195. package/src/utils/workflow/derive-workflow-plan.ts +0 -117
  196. package/src/utils/workflow/dsl/deserialize-dsl-workflow.ts +0 -1465
  197. package/src/utils/workflow/dsl/deserialize-fanout-body.test.ts +0 -71
  198. package/src/utils/workflow/dsl/deserialize-fanout-fidelity.test.ts +0 -99
  199. package/src/utils/workflow/dsl/deserialize-graph-next.test.ts +0 -161
  200. package/src/utils/workflow/dsl/deserialize-roundtrip-bindings.test.ts +0 -121
  201. package/src/utils/workflow/dsl/deserialize-roundtrip-values.test.ts +0 -116
  202. package/src/utils/workflow/dsl/deserialize-roundtrip.test.ts +0 -145
  203. package/src/utils/workflow/dsl/extract-approval-policy.test.ts +0 -71
  204. package/src/utils/workflow/dsl/extract-dsl-workflow.ts +0 -2254
  205. package/src/utils/workflow/dsl/index.ts +0 -11
  206. package/src/utils/workflow/dsl/patterns.ts +0 -440
  207. package/src/utils/workflow/dsl/validation.ts +0 -203
  208. package/src/utils/workflow/graph/convert-dsl-to-graph-skip.test.ts +0 -35
  209. package/src/utils/workflow/graph/convert-dsl-to-graph.ts +0 -543
  210. package/src/utils/workflow/graph/fanout-node-identity.test.ts +0 -105
  211. package/src/utils/workflow/graph/finalize-workflow-wires.ts +0 -310
  212. package/src/utils/workflow/graph/finalize-workflows.test.ts +0 -50
  213. package/src/utils/workflow/graph/finalize-workflows.ts +0 -130
  214. package/src/utils/workflow/graph/index.ts +0 -28
  215. package/src/utils/workflow/graph/serialize-workflow-graph.ts +0 -240
  216. package/src/utils/workflow/graph/workflow-graph.types.ts +0 -281
  217. package/src/visit.ts +0 -181
  218. package/tsconfig.json +0 -19
  219. package/tsconfig.tsbuildinfo +0 -1
@@ -1,791 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import { ErrorCode } from '../error-codes.js'
8
- import type { InspectorLogger } from '../types.js'
9
-
10
- const makeLogger = (criticals: Array<{ code: ErrorCode; message: string }>) =>
11
- ({
12
- debug: () => {},
13
- info: () => {},
14
- warn: () => {},
15
- error: () => {},
16
- diagnostic: ({ severity, code, message }) => {
17
- if (severity !== 'warn') {
18
- criticals.push({ code, message })
19
- }
20
- },
21
- critical: (code: ErrorCode, message: string) => {
22
- criticals.push({ code, message })
23
- },
24
- hasCriticalErrors: () => criticals.length > 0,
25
- }) satisfies InspectorLogger
26
-
27
- describe('addAuth inspector', () => {
28
- test('extracts socialProviders keys from the betterAuth call', async () => {
29
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-'))
30
- const file = join(rootDir, 'auth.ts')
31
-
32
- await writeFile(
33
- file,
34
- [
35
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
36
- "import { betterAuth } from 'better-auth'",
37
- 'export const auth = pikkuBetterAuth(() =>',
38
- ' betterAuth({',
39
- ' socialProviders: {',
40
- " github: { clientId: 'x', clientSecret: 'y' },",
41
- " google: { clientId: 'x', clientSecret: 'y' },",
42
- ' },',
43
- ' })',
44
- ')',
45
- ].join('\n')
46
- )
47
-
48
- const criticals: Array<{ code: ErrorCode; message: string }> = []
49
- try {
50
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
51
- assert.equal(criticals.length, 0)
52
- assert.deepEqual(state.auth.providers, ['github', 'google'])
53
- assert.deepEqual(state.auth.definition, {
54
- exportName: 'auth',
55
- sourceFile: file,
56
- basePath: '/api/auth',
57
- hasCredentials: false,
58
- cookieCache: false,
59
- plugins: [],
60
- services: { optimized: true, services: [] },
61
- })
62
- // The /api/auth/** routes are emitted into a generated auth.gen.ts; the
63
- // user's source file must NOT be imported into the HTTP bootstrap.
64
- assert.ok(
65
- !state.http.files.has(file),
66
- 'pikkuBetterAuth source file must not be added to http.files'
67
- )
68
- } finally {
69
- await rm(rootDir, { recursive: true, force: true })
70
- }
71
- })
72
-
73
- test('captures a custom basePath literal and credentials flag', async () => {
74
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-basepath-'))
75
- const file = join(rootDir, 'auth.ts')
76
-
77
- await writeFile(
78
- file,
79
- [
80
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
81
- "import { betterAuth } from 'better-auth'",
82
- 'export const auth = pikkuBetterAuth(() =>',
83
- ' betterAuth({',
84
- " basePath: '/auth',",
85
- ' emailAndPassword: { enabled: true },',
86
- " socialProviders: { github: { clientId: 'x', clientSecret: 'y' } },",
87
- ' })',
88
- ')',
89
- ].join('\n')
90
- )
91
-
92
- const criticals: Array<{ code: ErrorCode; message: string }> = []
93
- try {
94
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
95
- assert.equal(criticals.length, 0)
96
- assert.equal(state.auth.definition?.basePath, '/auth')
97
- assert.equal(state.auth.definition?.hasCredentials, true)
98
- assert.deepEqual(state.auth.providers, ['github'])
99
- } finally {
100
- await rm(rootDir, { recursive: true, force: true })
101
- }
102
- })
103
-
104
- test('detects session.cookieCache for the stateless middleware split', async () => {
105
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-cookiecache-'))
106
- const file = join(rootDir, 'auth.ts')
107
-
108
- await writeFile(
109
- file,
110
- [
111
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
112
- "import { betterAuth } from 'better-auth'",
113
- 'export const auth = pikkuBetterAuth(() =>',
114
- ' betterAuth({',
115
- ' session: { cookieCache: { enabled: true } },',
116
- ' emailAndPassword: { enabled: true },',
117
- ' })',
118
- ')',
119
- ].join('\n')
120
- )
121
-
122
- const criticals: Array<{ code: ErrorCode; message: string }> = []
123
- try {
124
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
125
- assert.equal(criticals.length, 0)
126
- assert.equal(state.auth.definition?.cookieCache, true)
127
- } finally {
128
- await rm(rootDir, { recursive: true, force: true })
129
- }
130
- })
131
-
132
- test('cookieCache: { enabled: false } does not enable the stateless split', async () => {
133
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-nocache-'))
134
- const file = join(rootDir, 'auth.ts')
135
-
136
- await writeFile(
137
- file,
138
- [
139
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
140
- "import { betterAuth } from 'better-auth'",
141
- 'export const auth = pikkuBetterAuth(() =>',
142
- ' betterAuth({',
143
- ' session: { cookieCache: { enabled: false } },',
144
- ' })',
145
- ')',
146
- ].join('\n')
147
- )
148
-
149
- const criticals: Array<{ code: ErrorCode; message: string }> = []
150
- try {
151
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
152
- assert.equal(criticals.length, 0)
153
- assert.equal(state.auth.definition?.cookieCache, false)
154
- } finally {
155
- await rm(rootDir, { recursive: true, force: true })
156
- }
157
- })
158
-
159
- test('cookieCache: { "enabled": false } (string-literal key) is honoured', async () => {
160
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-strkey-'))
161
- const file = join(rootDir, 'auth.ts')
162
-
163
- await writeFile(
164
- file,
165
- [
166
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
167
- "import { betterAuth } from 'better-auth'",
168
- 'export const auth = pikkuBetterAuth(() =>',
169
- ' betterAuth({',
170
- ' session: { cookieCache: { "enabled": false } },',
171
- ' })',
172
- ')',
173
- ].join('\n')
174
- )
175
-
176
- const criticals: Array<{ code: ErrorCode; message: string }> = []
177
- try {
178
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
179
- assert.equal(criticals.length, 0)
180
- assert.equal(state.auth.definition?.cookieCache, false)
181
- } finally {
182
- await rm(rootDir, { recursive: true, force: true })
183
- }
184
- })
185
-
186
- test('extracts plugin ids from the betterAuth plugins array', async () => {
187
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-plugins-'))
188
- const file = join(rootDir, 'auth.ts')
189
-
190
- await writeFile(
191
- file,
192
- [
193
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
194
- "import { betterAuth } from 'better-auth'",
195
- "import { bearer, apiKey, twoFactor } from 'better-auth/plugins'",
196
- 'export const auth = pikkuBetterAuth(() =>',
197
- ' betterAuth({',
198
- " socialProviders: { github: { clientId: 'x', clientSecret: 'y' } },",
199
- ' plugins: [bearer(), apiKey(), twoFactor({ issuer: "pikku" })],',
200
- ' })',
201
- ')',
202
- ].join('\n')
203
- )
204
-
205
- const criticals: Array<{ code: ErrorCode; message: string }> = []
206
- try {
207
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
208
- assert.equal(criticals.length, 0)
209
- assert.deepEqual(state.auth.plugins, ['bearer', 'apiKey', 'twoFactor'])
210
- assert.deepEqual(state.auth.definition?.plugins, [
211
- 'bearer',
212
- 'apiKey',
213
- 'twoFactor',
214
- ])
215
- } finally {
216
- await rm(rootDir, { recursive: true, force: true })
217
- }
218
- })
219
-
220
- test('refuses better-auth admin() and points at pikkuBan()', async () => {
221
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-admin-'))
222
- const file = join(rootDir, 'auth.ts')
223
-
224
- await writeFile(
225
- file,
226
- [
227
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
228
- "import { betterAuth } from 'better-auth'",
229
- "import { admin, bearer } from 'better-auth/plugins'",
230
- 'export const auth = pikkuBetterAuth(() =>',
231
- ' betterAuth({',
232
- ' plugins: [bearer(), admin()],',
233
- ' })',
234
- ')',
235
- ].join('\n')
236
- )
237
-
238
- const criticals: Array<{ code: ErrorCode; message: string }> = []
239
- try {
240
- await assert.rejects(
241
- () => inspect(makeLogger(criticals), [file], { rootDir }),
242
- (error: Error) => {
243
- assert.match(error.message, /admin\(\) plugin is not supported/)
244
- assert.match(error.message, /pikkuBan\(\)/)
245
- assert.match(error.message, /@pikku\/addon-admin/)
246
- return true
247
- }
248
- )
249
- } finally {
250
- await rm(rootDir, { recursive: true, force: true })
251
- }
252
- })
253
-
254
- test('refuses admin() reached through a namespace import', async () => {
255
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-ns-admin-'))
256
- const file = join(rootDir, 'auth.ts')
257
-
258
- await writeFile(
259
- file,
260
- [
261
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
262
- "import { betterAuth } from 'better-auth'",
263
- "import * as plugins from 'better-auth/plugins'",
264
- 'export const auth = pikkuBetterAuth(() =>',
265
- ' betterAuth({',
266
- ' plugins: [plugins.bearer(), plugins.admin()],',
267
- ' })',
268
- ')',
269
- ].join('\n')
270
- )
271
-
272
- const criticals: Array<{ code: ErrorCode; message: string }> = []
273
- try {
274
- await assert.rejects(
275
- () => inspect(makeLogger(criticals), [file], { rootDir }),
276
- /admin\(\) plugin is not supported/
277
- )
278
- } finally {
279
- await rm(rootDir, { recursive: true, force: true })
280
- }
281
- })
282
-
283
- test('allows a local plugin of its own named admin', async () => {
284
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-local-admin-'))
285
- const file = join(rootDir, 'auth.ts')
286
-
287
- await writeFile(
288
- file,
289
- [
290
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
291
- "import { betterAuth } from 'better-auth'",
292
- "import { bearer } from 'better-auth/plugins'",
293
- 'const admin = () => ({ id: "admin" })',
294
- 'export const auth = pikkuBetterAuth(() =>',
295
- ' betterAuth({',
296
- ' plugins: [bearer(), admin()],',
297
- ' })',
298
- ')',
299
- ].join('\n')
300
- )
301
-
302
- const criticals: Array<{ code: ErrorCode; message: string }> = []
303
- try {
304
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
305
- assert.equal(criticals.length, 0)
306
- assert.deepEqual(state.auth.plugins, ['bearer', 'admin'])
307
- } finally {
308
- await rm(rootDir, { recursive: true, force: true })
309
- }
310
- })
311
-
312
- test('records a namespace-imported plugin under its exported name', async () => {
313
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-ns-'))
314
- const file = join(rootDir, 'auth.ts')
315
-
316
- await writeFile(
317
- file,
318
- [
319
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
320
- "import { betterAuth } from 'better-auth'",
321
- "import * as plugins from 'better-auth/plugins'",
322
- 'export const auth = pikkuBetterAuth(() =>',
323
- ' betterAuth({',
324
- ' plugins: [plugins.bearer()],',
325
- ' })',
326
- ')',
327
- ].join('\n')
328
- )
329
-
330
- const criticals: Array<{ code: ErrorCode; message: string }> = []
331
- try {
332
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
333
- assert.equal(criticals.length, 0)
334
- assert.deepEqual(state.auth.plugins, ['bearer'])
335
- } finally {
336
- await rm(rootDir, { recursive: true, force: true })
337
- }
338
- })
339
-
340
- test('records no plugins when the plugins array is absent', async () => {
341
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-noplugins-'))
342
- const file = join(rootDir, 'auth.ts')
343
-
344
- await writeFile(
345
- file,
346
- [
347
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
348
- "import { betterAuth } from 'better-auth'",
349
- 'export const auth = pikkuBetterAuth(() => betterAuth({ emailAndPassword: { enabled: true } }))',
350
- ].join('\n')
351
- )
352
-
353
- const criticals: Array<{ code: ErrorCode; message: string }> = []
354
- try {
355
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
356
- assert.equal(criticals.length, 0)
357
- assert.deepEqual(state.auth.plugins, [])
358
- assert.deepEqual(state.auth.definition?.plugins, [])
359
- } finally {
360
- await rm(rootDir, { recursive: true, force: true })
361
- }
362
- })
363
-
364
- test('credentials-only (no socialProviders) records no providers', async () => {
365
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-creds-only-'))
366
- const file = join(rootDir, 'auth.ts')
367
-
368
- await writeFile(
369
- file,
370
- [
371
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
372
- "import { betterAuth } from 'better-auth'",
373
- 'export const auth = pikkuBetterAuth(() =>',
374
- ' betterAuth({ emailAndPassword: { enabled: true } })',
375
- ')',
376
- ].join('\n')
377
- )
378
-
379
- const criticals: Array<{ code: ErrorCode; message: string }> = []
380
- try {
381
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
382
- assert.equal(criticals.length, 0)
383
- assert.deepEqual(state.auth.providers, [])
384
- assert.equal(state.auth.definition?.hasCredentials, true)
385
- assert.ok(state.auth.files.has(file), 'source file still tracked')
386
- assert.ok(
387
- !state.http.files.has(file),
388
- 'pikkuBetterAuth source file must not be added to http.files'
389
- )
390
- } finally {
391
- await rm(rootDir, { recursive: true, force: true })
392
- }
393
- })
394
-
395
- test('errors when pikkuBetterAuth is not given a factory function', async () => {
396
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-nonfactory-'))
397
- const file = join(rootDir, 'auth.ts')
398
-
399
- await writeFile(
400
- file,
401
- [
402
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
403
- 'export const auth = pikkuBetterAuth({ providers: [] } as any)',
404
- ].join('\n')
405
- )
406
-
407
- const criticals: Array<{ code: ErrorCode; message: string }> = []
408
- try {
409
- await inspect(makeLogger(criticals), [file], { rootDir })
410
- const hit = criticals.find((e) => e.code === ErrorCode.MISSING_NAME)
411
- assert.ok(hit, 'expected MISSING_NAME critical for non-factory argument')
412
- } finally {
413
- await rm(rootDir, { recursive: true, force: true })
414
- }
415
- })
416
-
417
- test('errors when pikkuBetterAuth is not assigned to an exported const', async () => {
418
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-notexport-'))
419
- const file = join(rootDir, 'auth.ts')
420
-
421
- await writeFile(
422
- file,
423
- [
424
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
425
- "import { betterAuth } from 'better-auth'",
426
- 'const auth = pikkuBetterAuth(() => betterAuth({}))',
427
- ].join('\n')
428
- )
429
-
430
- const criticals: Array<{ code: ErrorCode; message: string }> = []
431
- try {
432
- await inspect(makeLogger(criticals), [file], { rootDir })
433
- const hit = criticals.find((e) => e.code === ErrorCode.AUTH_NOT_EXPORTED)
434
- assert.ok(hit, 'expected AUTH_NOT_EXPORTED critical')
435
- } finally {
436
- await rm(rootDir, { recursive: true, force: true })
437
- }
438
- })
439
-
440
- test('errors when pikkuBetterAuth is exported but not a const', async () => {
441
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-notconst-'))
442
- const file = join(rootDir, 'auth.ts')
443
-
444
- await writeFile(
445
- file,
446
- [
447
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
448
- "import { betterAuth } from 'better-auth'",
449
- 'export let auth = pikkuBetterAuth(() => betterAuth({}))',
450
- ].join('\n')
451
- )
452
-
453
- const criticals: Array<{ code: ErrorCode; message: string }> = []
454
- try {
455
- await inspect(makeLogger(criticals), [file], { rootDir })
456
- const hit = criticals.find((e) => e.code === ErrorCode.AUTH_NOT_EXPORTED)
457
- assert.ok(hit, 'expected AUTH_NOT_EXPORTED critical for non-const export')
458
- } finally {
459
- await rm(rootDir, { recursive: true, force: true })
460
- }
461
- })
462
-
463
- test('errors when more than one pikkuBetterAuth exists in the codebase', async () => {
464
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-dupe-'))
465
- const fileA = join(rootDir, 'auth.ts')
466
- const fileB = join(rootDir, 'auth-other.ts')
467
-
468
- await writeFile(
469
- fileA,
470
- [
471
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
472
- "import { betterAuth } from 'better-auth'",
473
- 'export const auth = pikkuBetterAuth(() => betterAuth({}))',
474
- ].join('\n')
475
- )
476
- await writeFile(
477
- fileB,
478
- [
479
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
480
- "import { betterAuth } from 'better-auth'",
481
- 'export const auth2 = pikkuBetterAuth(() => betterAuth({}))',
482
- ].join('\n')
483
- )
484
-
485
- const criticals: Array<{ code: ErrorCode; message: string }> = []
486
- try {
487
- await inspect(makeLogger(criticals), [fileA, fileB], { rootDir })
488
- const hit = criticals.find(
489
- (e) => e.code === ErrorCode.DUPLICATE_AUTH_DEFINITION
490
- )
491
- assert.ok(hit, 'expected DUPLICATE_AUTH_DEFINITION critical')
492
- } finally {
493
- await rm(rootDir, { recursive: true, force: true })
494
- }
495
- })
496
-
497
- test('extracts services from a destructured factory param', async () => {
498
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-svc-destr-'))
499
- const file = join(rootDir, 'auth.ts')
500
-
501
- await writeFile(
502
- file,
503
- [
504
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
505
- "import { betterAuth } from 'better-auth'",
506
- 'export const auth = pikkuBetterAuth(({ kysely, secrets }) =>',
507
- ' betterAuth({ database: { db: kysely, type: "postgres" }, secret: secrets })',
508
- ')',
509
- ].join('\n')
510
- )
511
-
512
- const criticals: Array<{ code: ErrorCode; message: string }> = []
513
- try {
514
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
515
- assert.equal(criticals.length, 0)
516
- assert.deepEqual(state.auth.definition?.services, {
517
- optimized: true,
518
- services: ['kysely', 'secrets'],
519
- })
520
- } finally {
521
- await rm(rootDir, { recursive: true, force: true })
522
- }
523
- })
524
-
525
- test('extracts services from the destructured factory param', async () => {
526
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-svc-member-'))
527
- const file = join(rootDir, 'auth.ts')
528
-
529
- await writeFile(
530
- file,
531
- [
532
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
533
- "import { betterAuth } from 'better-auth'",
534
- 'export const auth = pikkuBetterAuth(async ({ kysely, secrets }) =>',
535
- ' betterAuth({',
536
- ' database: { db: kysely, type: "postgres" },',
537
- " socialProviders: { github: await secrets.getSecret('GITHUB_OAUTH') },",
538
- ' })',
539
- ')',
540
- ].join('\n')
541
- )
542
-
543
- const criticals: Array<{ code: ErrorCode; message: string }> = []
544
- try {
545
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
546
- assert.equal(criticals.length, 0)
547
- assert.deepEqual(state.auth.definition?.services, {
548
- optimized: true,
549
- services: ['kysely', 'secrets'],
550
- })
551
- assert.deepEqual(state.auth.providers, ['github'])
552
- } finally {
553
- await rm(rootDir, { recursive: true, force: true })
554
- }
555
- })
556
-
557
- test('stamps the inspected services onto the generated handler meta', async () => {
558
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-stamp-'))
559
- const authFile = join(rootDir, 'auth.ts')
560
- const genFile = join(rootDir, 'auth.gen.ts')
561
-
562
- await writeFile(
563
- authFile,
564
- [
565
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
566
- "import { betterAuth } from 'better-auth'",
567
- 'export const auth = pikkuBetterAuth(({ kysely }) =>',
568
- ' betterAuth({ database: { db: kysely, type: "postgres" } })',
569
- ')',
570
- ].join('\n')
571
- )
572
- // Mimic the CLI-generated wiring file: the handler is a plain arrow (so the
573
- // inspector resolves a valid func and the routes are not skipped) that
574
- // delegates to createAuthHandler(...).func.
575
- await writeFile(
576
- genFile,
577
- [
578
- "import { pikkuSessionlessFunc } from '#pikku'",
579
- "import { wireHTTPRoutes } from '@pikku/core/http'",
580
- "import { createAuthHandler } from '@pikku/better-auth'",
581
- "import { auth } from './auth.js'",
582
- 'const authConfigHandler = createAuthHandler(auth)',
583
- 'export const authHandler = pikkuSessionlessFunc({',
584
- ' func: (services, data, interaction) =>',
585
- ' authConfigHandler.func(services, data, interaction),',
586
- '})',
587
- 'wireHTTPRoutes({',
588
- ' routes: {',
589
- " getAuthCatchAll: { method: 'get', route: '/api/auth{/*splat}', func: authHandler, auth: false },",
590
- ' },',
591
- '})',
592
- ].join('\n')
593
- )
594
-
595
- const criticals: Array<{ code: ErrorCode; message: string }> = []
596
- const errors: string[] = []
597
- const logger = makeLogger(criticals)
598
- logger.error = (message: string) => {
599
- errors.push(message)
600
- }
601
- try {
602
- const state = await inspect(logger, [authFile, genFile], { rootDir })
603
- assert.equal(criticals.length, 0, 'no critical diagnostics')
604
- assert.equal(
605
- errors.filter((e) => e.includes('No valid')).length,
606
- 0,
607
- `handler func must resolve; got: ${errors.join('; ')}`
608
- )
609
- const handlerMeta = state.functions.meta['authHandler']
610
- assert.ok(handlerMeta, 'generated authHandler must register a function')
611
- // The stamp (keyed on AUTH_HANDLER_FUNC_ID, ordered before aggregation)
612
- // overwrites the pass-through arrow's empty service list with the real deps.
613
- assert.deepEqual(handlerMeta.services, {
614
- optimized: true,
615
- services: ['kysely'],
616
- })
617
- } finally {
618
- await rm(rootDir, { recursive: true, force: true })
619
- }
620
- })
621
-
622
- test('tracks source file in state.auth.files', async () => {
623
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-files-'))
624
- const file = join(rootDir, 'auth.ts')
625
-
626
- await writeFile(
627
- file,
628
- [
629
- "import { pikkuBetterAuth } from '@pikku/better-auth'",
630
- "import { betterAuth } from 'better-auth'",
631
- "export const auth = pikkuBetterAuth(() => betterAuth({ socialProviders: { discord: { clientId: 'x', clientSecret: 'y' } } }))",
632
- ].join('\n')
633
- )
634
-
635
- const criticals: Array<{ code: ErrorCode; message: string }> = []
636
- try {
637
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
638
- assert.equal(criticals.length, 0)
639
- assert.ok(
640
- state.auth.files.has(file),
641
- 'source file should be tracked in state.auth.files'
642
- )
643
- } finally {
644
- await rm(rootDir, { recursive: true, force: true })
645
- }
646
- })
647
-
648
- test('user-registered betterAuthStatelessSession sets userStatelessSession', async () => {
649
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-stateless-'))
650
- const file = join(rootDir, 'middleware.ts')
651
- await writeFile(
652
- file,
653
- [
654
- "import { addHTTPMiddleware } from '#pikku'",
655
- "import { betterAuthStatelessSession } from '@pikku/better-auth'",
656
- "addHTTPMiddleware('*', [",
657
- ' betterAuthStatelessSession({',
658
- ' mapSession: (r: any) => ({ userId: r.user.id, role: r.user.role }),',
659
- ' }),',
660
- '])',
661
- ].join('\n')
662
- )
663
- const criticals: Array<{ code: ErrorCode; message: string }> = []
664
- try {
665
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
666
- assert.equal(state.auth.userStatelessSession, true)
667
- } finally {
668
- await rm(rootDir, { recursive: true, force: true })
669
- }
670
- })
671
-
672
- test('a standalone betterAuthStatelessSession() call (not a registration) does NOT set the flag', async () => {
673
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-standalone-'))
674
- const file = join(rootDir, 'start.ts')
675
- await writeFile(
676
- file,
677
- [
678
- "import { betterAuthStatelessSession } from '@pikku/better-auth'",
679
- '// harness use, not a global registration',
680
- 'const mw = betterAuthStatelessSession()',
681
- 'void mw',
682
- ].join('\n')
683
- )
684
- const criticals: Array<{ code: ErrorCode; message: string }> = []
685
- try {
686
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
687
- assert.ok(
688
- !state.auth.userStatelessSession,
689
- 'a bare call must not count as a registration'
690
- )
691
- } finally {
692
- await rm(rootDir, { recursive: true, force: true })
693
- }
694
- })
695
-
696
- test('a route-scoped addHTTPMiddleware registration does NOT set the flag', async () => {
697
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-scoped-'))
698
- const file = join(rootDir, 'middleware.ts')
699
- await writeFile(
700
- file,
701
- [
702
- "import { addHTTPMiddleware } from '#pikku'",
703
- "import { betterAuthStatelessSession } from '@pikku/better-auth'",
704
- "addHTTPMiddleware('/api/admin/*', [betterAuthStatelessSession()])",
705
- ].join('\n')
706
- )
707
- const criticals: Array<{ code: ErrorCode; message: string }> = []
708
- try {
709
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
710
- assert.ok(
711
- !state.auth.userStatelessSession,
712
- 'a route-scoped registration must not suppress the global generated middleware'
713
- )
714
- } finally {
715
- await rm(rootDir, { recursive: true, force: true })
716
- }
717
- })
718
-
719
- test('betterAuthStatelessSession in a .gen.ts file does NOT set the flag', async () => {
720
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-genonly-'))
721
- const file = join(rootDir, 'auth-middleware.gen.ts')
722
- await writeFile(
723
- file,
724
- [
725
- "import { addHTTPMiddleware } from '#pikku'",
726
- "import { betterAuthStatelessSession } from '@pikku/better-auth'",
727
- "addHTTPMiddleware('*', [betterAuthStatelessSession()])",
728
- ].join('\n')
729
- )
730
- const criticals: Array<{ code: ErrorCode; message: string }> = []
731
- try {
732
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
733
- assert.ok(
734
- !state.auth.userStatelessSession,
735
- 'generated file must not self-trigger the skip'
736
- )
737
- } finally {
738
- await rm(rootDir, { recursive: true, force: true })
739
- }
740
- })
741
-
742
- test('user-registered global betterAuthSession sets hasUserSessionMiddleware', async () => {
743
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-session-'))
744
- const file = join(rootDir, 'middleware.ts')
745
- await writeFile(
746
- file,
747
- [
748
- "import { addHTTPMiddleware } from '#pikku'",
749
- "import { betterAuthSession } from '@pikku/better-auth'",
750
- "addHTTPMiddleware('*', [",
751
- ' betterAuthSession({',
752
- ' impersonation: { loadUser: (id: string) => ({ id }) },',
753
- ' }),',
754
- '])',
755
- ].join('\n')
756
- )
757
- const criticals: Array<{ code: ErrorCode; message: string }> = []
758
- try {
759
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
760
- assert.equal(state.auth.hasUserSessionMiddleware, true)
761
- } finally {
762
- await rm(rootDir, { recursive: true, force: true })
763
- }
764
- })
765
-
766
- test('betterAuthSession in a .gen.ts file does NOT set hasUserSessionMiddleware', async () => {
767
- // Critical: the CLI's own auth.gen.ts contains addHTTPMiddleware('*',
768
- // [betterAuthSession()]). It must not count as a user registration, or it
769
- // would suppress itself and leave the chain with no session middleware.
770
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-session-gen-'))
771
- const file = join(rootDir, 'auth.gen.ts')
772
- await writeFile(
773
- file,
774
- [
775
- "import { addHTTPMiddleware } from '#pikku'",
776
- "import { betterAuthSession } from '@pikku/better-auth'",
777
- "addHTTPMiddleware('*', [betterAuthSession()])",
778
- ].join('\n')
779
- )
780
- const criticals: Array<{ code: ErrorCode; message: string }> = []
781
- try {
782
- const state = await inspect(makeLogger(criticals), [file], { rootDir })
783
- assert.ok(
784
- !state.auth.hasUserSessionMiddleware,
785
- 'generated file must not self-trigger the skip'
786
- )
787
- } finally {
788
- await rm(rootDir, { recursive: true, force: true })
789
- }
790
- })
791
- })