@pikku/inspector 0.12.19 → 0.12.21

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