Skip to content

armgit / fx0

Public Imported from GitHub
0

A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.

TypeScript
13.0 KB
Raw Blame
import assert from 'node:assert/strict'
import test from 'node:test'
import { memoryRedis, turnCredentialWith } from './fixtures/memory-redis.mjs'

// Every paid turn bills the login and team that sent the message starting it,
// whatever the chat's later messages and token renewals send.

const HOUR = 3_600_000

function useSecret(t) {
  const previous = process.env.SESSION_SECRET
  process.env.SESSION_SECRET = 'turn-funding-test-session-password'
  t.after(() => { if (previous === undefined) delete process.env.SESSION_SECRET; else process.env.SESSION_SECRET = previous })
}

function session(loginId, teamId, accessToken = `${loginId}-${teamId}`) {
  return { userId: 'user-a', loginId, teamId, accessToken, email: '[email protected]', loginAt: 1, isLoggedIn: true, expiresAt: Date.now() + HOUR }
}

// What a turn bills: [login, team, token], or why it bills nothing.
async function billed(credentials, turnId) {
  const funding = await credentials.readExecutionFunding('chat', turnId)
  return funding.ok ? [funding.credential.loginId, funding.credential.teamId, funding.credential.accessToken] : funding.refused
}

test('a paid turn keeps its sender\'s login and team when another login or team sends the next message', async (t) => {
  useSecret(t)
  const credentials = await turnCredentialWith(t, memoryRedis())
  await credentials.saveTurnCredential('chat', session('laptop', 'team_a'), 1)
  await credentials.saveTurnCredential('chat', session('phone', 'team_b'), 2)
  assert.deepEqual(await billed(credentials, 1), ['laptop', 'team_a', 'laptop-team_a'])
  assert.deepEqual(await billed(credentials, 2), ['phone', 'team_b', 'phone-team_b'])

  // The laptop switches teams. Its new token reaches its running turn, which
  // still bills the team its message chose.
  await credentials.saveTurnCredential('chat', session('laptop', 'team_b', 'laptop-new'), 3)
  assert.deepEqual(await billed(credentials, 1), ['laptop', 'team_a', 'laptop-new'])
  assert.deepEqual(await billed(credentials, 2), ['phone', 'team_b', 'phone-team_b'])
  assert.deepEqual(await billed(credentials, 3), ['laptop', 'team_b', 'laptop-new'])

  // A renewal from the phone reaches only the phone's turn.
  await credentials.renewLatestCredential('chat', session('phone', 'team_b', 'phone-renewed'))
  assert.deepEqual(await billed(credentials, 1), ['laptop', 'team_a', 'laptop-team_a'])
  assert.deepEqual(await billed(credentials, 2), ['phone', 'team_b', 'phone-renewed'])
  assert.deepEqual(await billed(credentials, 3), ['laptop', 'team_b', 'laptop-new'])
})

test('no later message or renewal changes who pays for an earlier turn', async (t) => {
  useSecret(t)
  const credentials = await turnCredentialWith(t, memoryRedis())
  let seed = 0x5eed
  const pick = (items) => { seed = (seed * 1103515245 + 12345) & 0x7fffffff; return items[seed % items.length] }
  for (let run = 0; run < 25; run++) {
    globalThis.__memoryRedis = memoryRedis().client
    const funders = new Map() // turn -> [login, team]
    const tokens = new Map() // login -> every token it stored
    let token = 0
    for (let step = 0; step < 6; step++) {
      const sent = session(pick(['laptop', 'phone', 'tablet']), pick(['team_a', 'team_b']), `token-${++token}`)
      tokens.set(sent.loginId, [...(tokens.get(sent.loginId) ?? []), sent.accessToken])
      if (funders.size && pick([true, false, false])) {
        await credentials.renewLatestCredential('chat', sent)
      } else {
        const turnId = funders.size + 1
        await credentials.saveTurnCredential('chat', sent, turnId)
        funders.set(turnId, [sent.loginId, sent.teamId])
      }
      for (const [turnId, [loginId, teamId]] of funders) {
        const where = `run ${run}, step ${step}, turn ${turnId}`
        const pays = await billed(credentials, turnId)
        assert.deepEqual(pays.slice(0, 2), [loginId, teamId], where)
        assert.ok(tokens.get(loginId).includes(pays[2]), `${where}: a token of another login`)
      }
    }
  }
})

test('a turn from before per-turn funding bills as it did then, and a later turn with no record bills nothing', async (t) => {
  useSecret(t)
  const credentials = await turnCredentialWith(t, memoryRedis())
  // Before this change a paid message stored only the chat's latest credential.
  await credentials.renewLatestCredential('chat', session('laptop', 'team_a'))
  assert.deepEqual(await billed(credentials, 4), ['laptop', 'team_a', 'laptop-team_a'], 'a turn running across the deploy')
  assert.deepEqual(await billed(credentials, undefined), ['laptop', 'team_a', 'laptop-team_a'])

  await credentials.saveTurnCredential('chat', session('phone', 'team_b'), 5)
  // Turn 4 was running or queued when the deploy landed: it bills the chat's
  // latest credential, as it would have before, so a follow-up never cuts it off.
  assert.deepEqual(await billed(credentials, 4), ['phone', 'team_b', 'phone-team_b'], 'the pre-deploy turn keeps running')
  assert.equal(await billed(credentials, 6), 'unbound-turn', 'every message since is pinned, so a later turn with no record bills nothing')
  assert.equal(await billed(credentials, undefined), 'no-turn')
  assert.deepEqual(await billed(credentials, 5), ['phone', 'team_b', 'phone-team_b'])
})

test('paid funding records stay out of the record the code before them reads, so a rollback bills as it did', async (t) => {
  useSecret(t)
  const store = memoryRedis()
  const credentials = await turnCredentialWith(t, store)
  await credentials.saveTurnCredential('chat', session('laptop', 'team_a'), 1)
  await credentials.saveTurnCredential('chat', session('phone', 'team_b'), 2)
  // The code before per-turn funding refuses a chat's unpinned paid turns once
  // `funding:` holds any record, and it pins no paid message.
  assert.equal(store.hashes.has('test:funding:chat'), false)
  assert.deepEqual(Object.keys(store.hashes.get('test:paid-funding:chat')), ['1', '2'])
  // Sponsored turns stay where that code reads them.
  const previous = { key: process.env.AI_GATEWAY_API_KEY, enabled: process.env.FX0_FREE_PROMPTS }
  Object.assign(process.env, { AI_GATEWAY_API_KEY: 'synthetic-server-key-never-store', FX0_FREE_PROMPTS: '1' })
  t.after(() => {
    for (const [name, value] of Object.entries({ AI_GATEWAY_API_KEY: previous.key, FX0_FREE_PROMPTS: previous.enabled })) {
      if (value === undefined) delete process.env[name]; else process.env[name] = value
    }
  })
  await credentials.saveTrialCredential('chat', { kind: 'trial', userId: 'user-a', loginId: 'laptop', expiresAt: Date.now() + HOUR }, 3)
  assert.deepEqual(Object.keys(store.hashes.get('test:funding:chat')), ['3'])
  // A paid record that code wrote in `funding:` (a trial chat's) still bills its turn.
  store.hashes.set('test:funding:chat', { ...store.hashes.get('test:funding:chat'), 1: store.hashes.get('test:paid-funding:chat')[1] })
  delete store.hashes.get('test:paid-funding:chat')[1]
  assert.deepEqual(await billed(credentials, 1), ['laptop', 'team_a', 'laptop-team_a'])
  assert.deepEqual(await billed(credentials, 2), ['phone', 'team_b', 'phone-team_b'])
  // Deleting the chat removes both kinds of record and every turn credential.
  await credentials.dropChatCredential('chat')
  assert.equal(store.hashes.size + store.values.size, 0)
})

test('a paid message whose credential cannot be stored is not admitted', async (t) => {
  useSecret(t)
  const store = memoryRedis()
  const credentials = await turnCredentialWith(t, store)
  await assert.rejects(credentials.saveTurnCredential('chat', { ...session('laptop', 'team_a'), expiresAt: Date.now() - 1 }, 1), /Sign in again/)
  await assert.rejects(credentials.saveTurnCredential('chat', { ...session('laptop', 'team_a'), teamId: '' }, 1), /Sign in again/)
  assert.equal(store.values.size + store.hashes.size, 0)
})

test('sign-out drops the departing login\'s turn credentials and keeps every funding record', async (t) => {
  useSecret(t)
  const store = memoryRedis()
  const credentials = await turnCredentialWith(t, store)
  await credentials.saveTurnCredential('chat', session('laptop', 'team_a'), 1)
  await credentials.saveTurnCredential('chat', session('phone', 'team_b'), 2)
  assert.deepEqual(await credentials.dropLoginCredentials('laptop'), [{ chatId: 'chat', turns: 1, latest: 'not-this-login' }])
  assert.equal(await billed(credentials, 1), 'unusable', 'no fallback to the latest credential')
  assert.deepEqual(await billed(credentials, 2), ['phone', 'team_b', 'phone-team_b'])
  assert.deepEqual(Object.keys(store.hashes.get('test:paid-funding:chat')), ['1', '2'])
  assert.deepEqual(await credentials.dropLoginCredentials('laptop'), [], 'a repeated sign-out finds nothing')
  assert.deepEqual(await credentials.dropLoginCredentials('phone'), [{ chatId: 'chat', turns: 1, latest: 'deleted' }])
  assert.equal(await billed(credentials, 2), 'unusable')
  assert.equal(await credentials.readTurnCredential('chat'), null)
})

test('sign-out keeps a latest credential another login stored after it looked', async (t) => {
  useSecret(t)
  const store = memoryRedis()
  const credentials = await turnCredentialWith(t, store)
  await credentials.saveTurnCredential('chat', session('laptop', 'team_a'), 1)
  // The phone's message lands between the laptop's sign-out reading the chat
  // and running its delete pipeline.
  const open = store.client.pipeline
  store.client.pipeline = () => {
    const pipeline = open()
    const { eval: script, exec } = pipeline
    let deletes = false
    pipeline.eval = (...args) => { deletes = true; return script(...args) }
    pipeline.exec = async () => {
      if (deletes) await credentials.saveTurnCredential('chat', session('phone', 'team_b'), 2)
      return exec()
    }
    return pipeline
  }
  const dropped = await credentials.dropLoginCredentials('laptop')
  assert.equal((await credentials.readTurnCredential('chat'))?.loginId, 'phone', 'the phone\'s credential survives')
  assert.deepEqual(dropped, [{ chatId: 'chat', turns: 1, latest: 'replaced' }])
  assert.equal(await billed(credentials, 1), 'unusable')
  assert.deepEqual(await billed(credentials, 2), ['phone', 'team_b', 'phone-team_b'])
})

test('sign-out cleanup names only the departing login\'s turns, and the latest credential only if it is that login\'s', async (t) => {
  const { signOutCleanup } = await turnCredentialWith(t, memoryRedis())
  const record = (kind, loginId) => JSON.stringify({ kind, userId: 'user-a', loginId, ...(kind === 'user' ? { teamId: 'team_a' } : {}) })
  const bindings = { 1: record('trial', 'laptop'), 2: record('user', 'phone'), 3: record('user', 'laptop'), 4: 'not a record' }
  const user = (loginId) => ({ userId: 'user-a', loginId, accessToken: 'token', teamId: 'team_a', expiresAt: Date.now() + HOUR })
  assert.deepEqual(plan(signOutCleanup('laptop', bindings, 'sealed-laptop', user('laptop'))), [[1, 3], 'sealed-laptop'])
  assert.deepEqual(plan(signOutCleanup('laptop', bindings, 'sealed-phone', user('phone'))), [[1, 3], null])
  assert.deepEqual(plan(signOutCleanup('phone', bindings, 'sealed-laptop', user('laptop'))), [[2], null])
  assert.deepEqual(plan(signOutCleanup('tablet', bindings, null, null)), [[], null])
  // A seal that no longer opens, such as an expired one, names nothing to compare.
  assert.deepEqual(plan(signOutCleanup('laptop', {}, 'expired-seal', null)), [[], null])
})

const plan = ({ turnIds, latestSeal }) => [turnIds, latestSeal]

test('resolution bills a turn\'s recorded identity and team, or nothing', async (t) => {
  const { resolveFunding } = await turnCredentialWith(t, memoryRedis())
  const user = (loginId, teamId) => ({ userId: 'user-a', loginId, teamId, accessToken: `${loginId}-${teamId}`, expiresAt: Date.now() + HOUR })
  const trial = (loginId) => ({ kind: 'trial', userId: 'user-a', loginId, expiresAt: Date.now() + HOUR })
  const credentials = [null, trial('laptop'), trial('phone'), user('laptop', 'team_a'), user('laptop', 'team_b'), user('phone', 'team_a')]
  const bindings = [null, { kind: 'trial', userId: 'user-a', loginId: 'laptop' }, { kind: 'user', userId: 'user-a', loginId: 'laptop', teamId: 'team_a' }]
  for (const records of [0, 1]) for (const turnId of [null, 7]) for (const binding of bindings) for (const firstFunded of [null, 5, 9]) {
    for (const exact of credentials) for (const latest of credentials) {
      const where = JSON.stringify({ records, turnId, binding, firstFunded, exact, latest })
      const funding = resolveFunding(records, turnId, binding, exact, latest, firstFunded)
      // A chat with no records, or a turn with none that is older than the chat's
      // first, bills the latest user credential.
      if (records === 0 || (turnId !== null && !binding && firstFunded !== null && turnId < firstFunded)) {
        assert.equal(funding.ok, !!latest && !('kind' in latest), where)
        if (funding.ok) assert.equal(funding.credential, latest, where)
        continue
      }
      if (!funding.ok) continue
      assert.ok(turnId !== null && binding, where)
      assert.deepEqual([funding.credential.userId, funding.credential.loginId], [binding.userId, binding.loginId], where)
      assert.equal('kind' in funding.credential, binding.kind === 'trial', where)
      if (binding.kind === 'user') assert.equal(funding.credential.teamId, binding.teamId, where)
    }
  }
})