@pikku/inspector 0.12.19 → 0.12.20

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.
@@ -20,15 +20,23 @@ const makeLogger = (criticals: Array<{ code: ErrorCode; message: string }>) =>
20
20
  }) satisfies InspectorLogger
21
21
 
22
22
  describe('addAuth inspector', () => {
23
- test('extracts provider string literals from wireAuth call', async () => {
23
+ test('extracts socialProviders keys from the betterAuth call', async () => {
24
24
  const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-'))
25
25
  const file = join(rootDir, 'auth.ts')
26
26
 
27
27
  await writeFile(
28
28
  file,
29
29
  [
30
- "import { wireAuth } from '@pikku/auth-js'",
31
- "wireAuth({ providers: ['github', 'google'] })",
30
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
31
+ "import { betterAuth } from 'better-auth'",
32
+ 'export const auth = pikkuBetterAuth(() =>',
33
+ ' betterAuth({',
34
+ ' socialProviders: {',
35
+ " github: { clientId: 'x', clientSecret: 'y' },",
36
+ " google: { clientId: 'x', clientSecret: 'y' },",
37
+ ' },',
38
+ ' })',
39
+ ')',
32
40
  ].join('\n')
33
41
  )
34
42
 
@@ -37,21 +45,41 @@ describe('addAuth inspector', () => {
37
45
  const state = await inspect(makeLogger(criticals), [file], { rootDir })
38
46
  assert.equal(criticals.length, 0)
39
47
  assert.deepEqual(state.auth.providers, ['github', 'google'])
48
+ assert.deepEqual(state.auth.definition, {
49
+ exportName: 'auth',
50
+ sourceFile: file,
51
+ basePath: '/api/auth',
52
+ hasCredentials: false,
53
+ plugins: [],
54
+ services: { optimized: true, services: [] },
55
+ })
56
+ // The /api/auth/** routes are emitted into a generated auth.gen.ts; the
57
+ // user's source file must NOT be imported into the HTTP bootstrap.
58
+ assert.ok(
59
+ !state.http.files.has(file),
60
+ 'pikkuBetterAuth source file must not be added to http.files'
61
+ )
40
62
  } finally {
41
63
  await rm(rootDir, { recursive: true, force: true })
42
64
  }
43
65
  })
44
66
 
45
- test('deduplicates providers across multiple wireAuth calls', async () => {
46
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-dedup-'))
67
+ test('captures a custom basePath literal and credentials flag', async () => {
68
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-basepath-'))
47
69
  const file = join(rootDir, 'auth.ts')
48
70
 
49
71
  await writeFile(
50
72
  file,
51
73
  [
52
- "import { wireAuth } from '@pikku/auth-js'",
53
- "wireAuth({ providers: ['github'] })",
54
- "wireAuth({ providers: ['github', 'google'] })",
74
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
75
+ "import { betterAuth } from 'better-auth'",
76
+ 'export const auth = pikkuBetterAuth(() =>',
77
+ ' betterAuth({',
78
+ " basePath: '/auth',",
79
+ ' emailAndPassword: { enabled: true },',
80
+ " socialProviders: { github: { clientId: 'x', clientSecret: 'y' } },",
81
+ ' })',
82
+ ')',
55
83
  ].join('\n')
56
84
  )
57
85
 
@@ -59,79 +87,112 @@ describe('addAuth inspector', () => {
59
87
  try {
60
88
  const state = await inspect(makeLogger(criticals), [file], { rootDir })
61
89
  assert.equal(criticals.length, 0)
62
- assert.deepEqual(state.auth.providers, ['github', 'google'])
90
+ assert.equal(state.auth.definition?.basePath, '/auth')
91
+ assert.equal(state.auth.definition?.hasCredentials, true)
92
+ assert.deepEqual(state.auth.providers, ['github'])
63
93
  } finally {
64
94
  await rm(rootDir, { recursive: true, force: true })
65
95
  }
66
96
  })
67
97
 
68
- test('logs critical error when a provider is a non-literal reference', async () => {
69
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-nonlit-'))
98
+ test('extracts plugin ids from the betterAuth plugins array', async () => {
99
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-plugins-'))
70
100
  const file = join(rootDir, 'auth.ts')
71
101
 
72
102
  await writeFile(
73
103
  file,
74
104
  [
75
- "import { wireAuth } from '@pikku/auth-js'",
76
- "const PROVIDER = 'github'",
77
- 'wireAuth({ providers: [PROVIDER] })',
105
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
106
+ "import { betterAuth } from 'better-auth'",
107
+ "import { bearer, admin, twoFactor } from 'better-auth/plugins'",
108
+ 'export const auth = pikkuBetterAuth(() =>',
109
+ ' betterAuth({',
110
+ " socialProviders: { github: { clientId: 'x', clientSecret: 'y' } },",
111
+ ' plugins: [bearer(), admin(), twoFactor({ issuer: "pikku" })],',
112
+ ' })',
113
+ ')',
78
114
  ].join('\n')
79
115
  )
80
116
 
81
117
  const criticals: Array<{ code: ErrorCode; message: string }> = []
82
118
  try {
83
- await inspect(makeLogger(criticals), [file], { rootDir })
84
- const hit = criticals.find(
85
- (e) => e.code === ErrorCode.NON_LITERAL_WIRE_NAME
86
- )
87
- assert.ok(hit, 'expected NON_LITERAL_WIRE_NAME critical')
88
- assert.match(hit!.message, /PROVIDER/)
119
+ const state = await inspect(makeLogger(criticals), [file], { rootDir })
120
+ assert.equal(criticals.length, 0)
121
+ assert.deepEqual(state.auth.plugins, ['bearer', 'admin', 'twoFactor'])
122
+ assert.deepEqual(state.auth.definition?.plugins, [
123
+ 'bearer',
124
+ 'admin',
125
+ 'twoFactor',
126
+ ])
89
127
  } finally {
90
128
  await rm(rootDir, { recursive: true, force: true })
91
129
  }
92
130
  })
93
131
 
94
- test('does not error when providers is absent (credentials-only wireAuth)', async () => {
132
+ test('records no plugins when the plugins array is absent', async () => {
133
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-noplugins-'))
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(() => betterAuth({ emailAndPassword: { enabled: true } }))',
142
+ ].join('\n')
143
+ )
144
+
145
+ const criticals: Array<{ code: ErrorCode; message: string }> = []
146
+ try {
147
+ const state = await inspect(makeLogger(criticals), [file], { rootDir })
148
+ assert.equal(criticals.length, 0)
149
+ assert.deepEqual(state.auth.plugins, [])
150
+ assert.deepEqual(state.auth.definition?.plugins, [])
151
+ } finally {
152
+ await rm(rootDir, { recursive: true, force: true })
153
+ }
154
+ })
155
+
156
+ test('credentials-only (no socialProviders) records no providers', async () => {
95
157
  const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-creds-only-'))
96
158
  const file = join(rootDir, 'auth.ts')
97
159
 
98
160
  await writeFile(
99
161
  file,
100
162
  [
101
- "import { wireAuth } from '@pikku/auth-js'",
102
- 'wireAuth({ credentials: { authorize: async () => null } })',
163
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
164
+ "import { betterAuth } from 'better-auth'",
165
+ 'export const auth = pikkuBetterAuth(() =>',
166
+ ' betterAuth({ emailAndPassword: { enabled: true } })',
167
+ ')',
103
168
  ].join('\n')
104
169
  )
105
170
 
106
171
  const criticals: Array<{ code: ErrorCode; message: string }> = []
107
172
  try {
108
173
  const state = await inspect(makeLogger(criticals), [file], { rootDir })
109
- assert.equal(
110
- criticals.length,
111
- 0,
112
- 'credentials-only wireAuth must not produce errors'
113
- )
114
- assert.deepEqual(
115
- state.auth.providers,
116
- [],
117
- 'no providers should be extracted'
118
- )
174
+ assert.equal(criticals.length, 0)
175
+ assert.deepEqual(state.auth.providers, [])
176
+ assert.equal(state.auth.definition?.hasCredentials, true)
119
177
  assert.ok(state.auth.files.has(file), 'source file still tracked')
178
+ assert.ok(
179
+ !state.http.files.has(file),
180
+ 'pikkuBetterAuth source file must not be added to http.files'
181
+ )
120
182
  } finally {
121
183
  await rm(rootDir, { recursive: true, force: true })
122
184
  }
123
185
  })
124
186
 
125
- test('logs critical error when providers is not an array literal', async () => {
126
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-nonarray-'))
187
+ test('errors when pikkuBetterAuth is not given a factory function', async () => {
188
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-nonfactory-'))
127
189
  const file = join(rootDir, 'auth.ts')
128
190
 
129
191
  await writeFile(
130
192
  file,
131
193
  [
132
- "import { wireAuth } from '@pikku/auth-js'",
133
- "const PROVIDERS = ['github']",
134
- 'wireAuth({ providers: PROVIDERS })',
194
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
195
+ 'export const auth = pikkuBetterAuth({ providers: [] } as any)',
135
196
  ].join('\n')
136
197
  )
137
198
 
@@ -139,10 +200,212 @@ describe('addAuth inspector', () => {
139
200
  try {
140
201
  await inspect(makeLogger(criticals), [file], { rootDir })
141
202
  const hit = criticals.find((e) => e.code === ErrorCode.MISSING_NAME)
142
- assert.ok(
143
- hit,
144
- 'expected MISSING_NAME critical for non-array-literal providers'
203
+ assert.ok(hit, 'expected MISSING_NAME critical for non-factory argument')
204
+ } finally {
205
+ await rm(rootDir, { recursive: true, force: true })
206
+ }
207
+ })
208
+
209
+ test('errors when pikkuBetterAuth is not assigned to an exported const', async () => {
210
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-notexport-'))
211
+ const file = join(rootDir, 'auth.ts')
212
+
213
+ await writeFile(
214
+ file,
215
+ [
216
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
217
+ "import { betterAuth } from 'better-auth'",
218
+ 'const auth = pikkuBetterAuth(() => betterAuth({}))',
219
+ ].join('\n')
220
+ )
221
+
222
+ const criticals: Array<{ code: ErrorCode; message: string }> = []
223
+ try {
224
+ await inspect(makeLogger(criticals), [file], { rootDir })
225
+ const hit = criticals.find((e) => e.code === ErrorCode.AUTH_NOT_EXPORTED)
226
+ assert.ok(hit, 'expected AUTH_NOT_EXPORTED critical')
227
+ } finally {
228
+ await rm(rootDir, { recursive: true, force: true })
229
+ }
230
+ })
231
+
232
+ test('errors when pikkuBetterAuth is exported but not a const', async () => {
233
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-notconst-'))
234
+ const file = join(rootDir, 'auth.ts')
235
+
236
+ await writeFile(
237
+ file,
238
+ [
239
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
240
+ "import { betterAuth } from 'better-auth'",
241
+ 'export let auth = pikkuBetterAuth(() => betterAuth({}))',
242
+ ].join('\n')
243
+ )
244
+
245
+ const criticals: Array<{ code: ErrorCode; message: string }> = []
246
+ try {
247
+ await inspect(makeLogger(criticals), [file], { rootDir })
248
+ const hit = criticals.find((e) => e.code === ErrorCode.AUTH_NOT_EXPORTED)
249
+ assert.ok(hit, 'expected AUTH_NOT_EXPORTED critical for non-const export')
250
+ } finally {
251
+ await rm(rootDir, { recursive: true, force: true })
252
+ }
253
+ })
254
+
255
+ test('errors when more than one pikkuBetterAuth exists in the codebase', async () => {
256
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-dupe-'))
257
+ const fileA = join(rootDir, 'auth.ts')
258
+ const fileB = join(rootDir, 'auth-other.ts')
259
+
260
+ await writeFile(
261
+ fileA,
262
+ [
263
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
264
+ "import { betterAuth } from 'better-auth'",
265
+ 'export const auth = pikkuBetterAuth(() => betterAuth({}))',
266
+ ].join('\n')
267
+ )
268
+ await writeFile(
269
+ fileB,
270
+ [
271
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
272
+ "import { betterAuth } from 'better-auth'",
273
+ 'export const auth2 = pikkuBetterAuth(() => betterAuth({}))',
274
+ ].join('\n')
275
+ )
276
+
277
+ const criticals: Array<{ code: ErrorCode; message: string }> = []
278
+ try {
279
+ await inspect(makeLogger(criticals), [fileA, fileB], { rootDir })
280
+ const hit = criticals.find(
281
+ (e) => e.code === ErrorCode.DUPLICATE_AUTH_DEFINITION
145
282
  )
283
+ assert.ok(hit, 'expected DUPLICATE_AUTH_DEFINITION critical')
284
+ } finally {
285
+ await rm(rootDir, { recursive: true, force: true })
286
+ }
287
+ })
288
+
289
+ test('extracts services from a destructured factory param', async () => {
290
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-svc-destr-'))
291
+ const file = join(rootDir, 'auth.ts')
292
+
293
+ await writeFile(
294
+ file,
295
+ [
296
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
297
+ "import { betterAuth } from 'better-auth'",
298
+ 'export const auth = pikkuBetterAuth(({ kysely, secrets }) =>',
299
+ ' betterAuth({ database: { db: kysely, type: "postgres" }, secret: secrets })',
300
+ ')',
301
+ ].join('\n')
302
+ )
303
+
304
+ const criticals: Array<{ code: ErrorCode; message: string }> = []
305
+ try {
306
+ const state = await inspect(makeLogger(criticals), [file], { rootDir })
307
+ assert.equal(criticals.length, 0)
308
+ assert.deepEqual(state.auth.definition?.services, {
309
+ optimized: true,
310
+ services: ['kysely', 'secrets'],
311
+ })
312
+ } finally {
313
+ await rm(rootDir, { recursive: true, force: true })
314
+ }
315
+ })
316
+
317
+ test('extracts services from the destructured factory param', async () => {
318
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-svc-member-'))
319
+ const file = join(rootDir, 'auth.ts')
320
+
321
+ await writeFile(
322
+ file,
323
+ [
324
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
325
+ "import { betterAuth } from 'better-auth'",
326
+ 'export const auth = pikkuBetterAuth(async ({ kysely, secrets }) =>',
327
+ ' betterAuth({',
328
+ ' database: { db: kysely, type: "postgres" },',
329
+ " socialProviders: { github: await secrets.getSecret('GITHUB_OAUTH') },",
330
+ ' })',
331
+ ')',
332
+ ].join('\n')
333
+ )
334
+
335
+ const criticals: Array<{ code: ErrorCode; message: string }> = []
336
+ try {
337
+ const state = await inspect(makeLogger(criticals), [file], { rootDir })
338
+ assert.equal(criticals.length, 0)
339
+ assert.deepEqual(state.auth.definition?.services, {
340
+ optimized: true,
341
+ services: ['kysely', 'secrets'],
342
+ })
343
+ assert.deepEqual(state.auth.providers, ['github'])
344
+ } finally {
345
+ await rm(rootDir, { recursive: true, force: true })
346
+ }
347
+ })
348
+
349
+ test('stamps the inspected services onto the generated handler meta', async () => {
350
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-stamp-'))
351
+ const authFile = join(rootDir, 'auth.ts')
352
+ const genFile = join(rootDir, 'auth.gen.ts')
353
+
354
+ await writeFile(
355
+ authFile,
356
+ [
357
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
358
+ "import { betterAuth } from 'better-auth'",
359
+ 'export const auth = pikkuBetterAuth(({ kysely }) =>',
360
+ ' betterAuth({ database: { db: kysely, type: "postgres" } })',
361
+ ')',
362
+ ].join('\n')
363
+ )
364
+ // Mimic the CLI-generated wiring file: the handler is a plain arrow (so the
365
+ // inspector resolves a valid func and the routes are not skipped) that
366
+ // delegates to createAuthHandler(...).func.
367
+ await writeFile(
368
+ genFile,
369
+ [
370
+ "import { pikkuSessionlessFunc } from '#pikku'",
371
+ "import { wireHTTPRoutes } from '@pikku/core/http'",
372
+ "import { createAuthHandler } from '@pikku/better-auth'",
373
+ "import { auth } from './auth.js'",
374
+ 'const authConfigHandler = createAuthHandler(auth)',
375
+ 'export const authHandler = pikkuSessionlessFunc({',
376
+ ' func: (services, data, interaction) =>',
377
+ ' authConfigHandler.func(services, data, interaction),',
378
+ '})',
379
+ 'wireHTTPRoutes({',
380
+ ' routes: {',
381
+ " getAuthCatchAll: { method: 'get', route: '/api/auth{/*splat}', func: authHandler, auth: false },",
382
+ ' },',
383
+ '})',
384
+ ].join('\n')
385
+ )
386
+
387
+ const criticals: Array<{ code: ErrorCode; message: string }> = []
388
+ const errors: string[] = []
389
+ const logger = makeLogger(criticals)
390
+ logger.error = (message: string) => {
391
+ errors.push(message)
392
+ }
393
+ try {
394
+ const state = await inspect(logger, [authFile, genFile], { rootDir })
395
+ assert.equal(criticals.length, 0, 'no critical diagnostics')
396
+ assert.equal(
397
+ errors.filter((e) => e.includes('No valid')).length,
398
+ 0,
399
+ `handler func must resolve; got: ${errors.join('; ')}`
400
+ )
401
+ const handlerMeta = state.functions.meta['authHandler']
402
+ assert.ok(handlerMeta, 'generated authHandler must register a function')
403
+ // The stamp (keyed on AUTH_HANDLER_FUNC_ID, ordered before aggregation)
404
+ // overwrites the pass-through arrow's empty service list with the real deps.
405
+ assert.deepEqual(handlerMeta.services, {
406
+ optimized: true,
407
+ services: ['kysely'],
408
+ })
146
409
  } finally {
147
410
  await rm(rootDir, { recursive: true, force: true })
148
411
  }
@@ -150,13 +413,14 @@ describe('addAuth inspector', () => {
150
413
 
151
414
  test('tracks source file in state.auth.files', async () => {
152
415
  const rootDir = await mkdtemp(join(tmpdir(), 'pikku-add-auth-files-'))
153
- const file = join(rootDir, 'auth.wiring.ts')
416
+ const file = join(rootDir, 'auth.ts')
154
417
 
155
418
  await writeFile(
156
419
  file,
157
420
  [
158
- "import { wireAuth } from '@pikku/auth-js'",
159
- "wireAuth({ providers: ['discord'] })",
421
+ "import { pikkuBetterAuth } from '@pikku/better-auth'",
422
+ "import { betterAuth } from 'better-auth'",
423
+ "export const auth = pikkuBetterAuth(() => betterAuth({ socialProviders: { discord: { clientId: 'x', clientSecret: 'y' } } }))",
160
424
  ].join('\n')
161
425
  )
162
426