const test = require('node:test')
const assert = require('node:assert/strict')
const { firebaseUidFromIdToken } = require('../src/lib/id-token')

test('firebaseUidFromIdToken reads sub', () => {
  const payload = Buffer.from(JSON.stringify({ sub: 'uid-from-sub' })).toString('base64url')
  const tok = `xx.${payload}.yy`
  assert.equal(firebaseUidFromIdToken(tok), 'uid-from-sub')
})

test('firebaseUidFromIdToken falls back to user_id', () => {
  const payload = Buffer.from(JSON.stringify({ user_id: 'legacy-uid' })).toString('base64url')
  assert.equal(firebaseUidFromIdToken(`a.${payload}.b`), 'legacy-uid')
})

test('firebaseUidFromIdToken returns null for garbage', () => {
  assert.equal(firebaseUidFromIdToken(''), null)
  assert.equal(firebaseUidFromIdToken('not-a-jwt'), null)
  assert.equal(firebaseUidFromIdToken(null), null)
})
