Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
c19c893
refactor(account): make hosts the single source of auth copy
MaanilVerma Sep 14, 2026
e211cd3
fix(locales): drop account-existence wording from email-already-in-use
MaanilVerma Sep 14, 2026
64ece9e
test(auth): guard cloud auth copy against account enumeration
MaanilVerma Sep 14, 2026
3aa6c3c
test(auth): address review nits on auth copy resolution
MaanilVerma Sep 14, 2026
36c5790
test(website): assert the collapsed invalid-credential line on user-n…
MaanilVerma Sep 15, 2026
c66a653
fix(website): drop the unrenderable user-not-found/wrong-password copy
MaanilVerma Sep 15, 2026
9348238
test(website): broaden the sign-in enumeration oracle to match the cl…
MaanilVerma Sep 15, 2026
b742c75
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma Sep 15, 2026
7184764
fix(locales): drop the unrenderable user-not-found/wrong-password copy
MaanilVerma Sep 15, 2026
d0e3c13
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma Sep 15, 2026
3633eb7
test(website): use the i18n generic-error copy after the main merge
MaanilVerma Sep 15, 2026
cea548a
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma Sep 15, 2026
61b4451
fix(workspace): type the session error key against the locale shape
MaanilVerma Sep 15, 2026
b7a3541
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma Sep 15, 2026
885faf4
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma Sep 15, 2026
e68e3f0
Merge remote-tracking branch 'origin/main' into maanil/fe-2188-locali…
MaanilVerma Sep 15, 2026
4db1dbe
Merge remote-tracking branch 'origin/main' into maanil/fe-2191-decomp…
MaanilVerma Sep 16, 2026
d69670b
refactor(account): add pure session state transition and mint arbitra…
MaanilVerma Sep 16, 2026
7a16e1f
refactor(account): route every session commit through one transition
MaanilVerma Sep 16, 2026
6c9c0a5
refactor(account): import AccountUser from sessionContracts in identity
MaanilVerma Sep 16, 2026
d5e9fc3
refactor(account): carry commit payloads in session events and effects
MaanilVerma Sep 16, 2026
4878540
refactor(account): extract credential cache and mint coordinator from…
MaanilVerma Sep 16, 2026
3ccca0d
refactor(account): keep credential freshness policy with the cache
MaanilVerma Sep 16, 2026
821bdf6
refactor(account): route the expiry commit through the single commit …
MaanilVerma Sep 16, 2026
f7e2e23
Merge remote-tracking branch 'origin/main' into maanil/fe-2191-decomp…
MaanilVerma Sep 16, 2026
9874cfe
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma Sep 16, 2026
651e277
Merge branch 'maanil/fe-2188-localization-sso' into maanil/fe-2191-de…
MaanilVerma Sep 16, 2026
71e38b3
refactor(account): split the session transition and mint arbitration …
MaanilVerma Sep 16, 2026
612a1bb
fix(account): tighten the popup mint to exactly one identity event
MaanilVerma Sep 16, 2026
44bbc19
perf(account): read stored credentials only when the in-memory one is…
MaanilVerma Sep 16, 2026
23c5bb6
test(auth): share the enumeration oracle and cover the cancelled seco…
MaanilVerma Sep 16, 2026
df8ea1d
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma Sep 16, 2026
82e272e
Merge remote-tracking branch 'origin/maanil/fe-2188-localization-sso'…
MaanilVerma Sep 16, 2026
448c4b9
Merge remote-tracking branch 'origin/main' into maanil/fe-2188-locali…
MaanilVerma Sep 17, 2026
52054e1
Merge branch 'maanil/fe-2188-localization-sso' into maanil/fe-2191-de…
MaanilVerma Sep 17, 2026
386d96d
Merge remote-tracking branch 'origin/main' into maanil/fe-2188-locali…
MaanilVerma Sep 17, 2026
ffbec33
Merge branch 'maanil/fe-2188-localization-sso' into maanil/fe-2191-de…
MaanilVerma Sep 17, 2026
df6ffd7
refactor(account): name the mint guards by verdict and split the type…
MaanilVerma Sep 17, 2026
d48db49
test(account): share the session test fakes from one fixture module
MaanilVerma Sep 17, 2026
9826d1b
Merge remote-tracking branch 'origin/main' into maanil/fe-2188-locali…
MaanilVerma Sep 17, 2026
f8e97d4
Merge branch 'maanil/fe-2188-localization-sso' into maanil/fe-2191-de…
MaanilVerma Sep 17, 2026
feaeccc
test(account): split the client builder out of the data fixture and d…
MaanilVerma Sep 17, 2026
112bec2
test(account): import the scheduler buffer default instead of copying it
MaanilVerma Sep 17, 2026
4fbdc9b
fix(account): drop a session subscriber whose first replay throws
MaanilVerma Sep 17, 2026
7614f52
Merge remote-tracking branch 'origin/main' into maanil/fe-2191-decomp…
MaanilVerma Sep 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* The session client builder. Kept apart from `sessionFakes.ts` so the
* pure-module suites (credential cache, mint coordinator, state machine) that
* only need the data fakes do not evaluate the whole client graph.
*/
import type { SessionClientOptions } from '../session.js'
import { createSessionClient } from '../session.js'

import { EXCHANGE_URL, memoryStorage } from './sessionFakes.js'

export function makeClient(overrides: Partial<SessionClientOptions> = {}) {
const storage = memoryStorage()
const client = createSessionClient({
exchangeUrl: EXCHANGE_URL,
storage,
...overrides
Comment thread
MaanilVerma marked this conversation as resolved.
Resolved
})
return { client, storage }
}
98 changes: 98 additions & 0 deletions packages/account-core/src/core/__fixtures__/sessionFakes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* The storage, identity, user, credential and exchange fakes the session
* suites build on. A suite keeps what only it needs.
*/
import { vi } from 'vitest'

import { createTestIdentity } from '../../testing.js'
import type {
AccountCredential,
AccountUser,
CredentialStorage
} from '../session.js'

export const EXCHANGE_URL = 'https://cloud.test/api/auth/token'
export const NINETY_MINUTES_MS = 90 * 60 * 1000

export function memoryStorage(): CredentialStorage & {
raw: () => string | null
} {
let value: string | null = null
return {
read: () => value,
write: (next) => {
value = next
},
clear: () => {
value = null
},
raw: () => value
}
}

export function testUser(uid = 'uid-1', idToken = 'id-token-1'): AccountUser {
return { uid, getIdToken: vi.fn(async () => idToken) }
}

export function credential(
token: string,
overrides: Partial<AccountCredential> = {}
): AccountCredential {
return {
token,
expiresAt: 1_000_000,
uid: 'uid-1',
workspace: { id: 'ws-1', name: 'Personal', type: 'personal' },
role: 'owner',
permissions: ['workspace:read'],
...overrides
}
}

export function deferred<T>() {
let resolve!: (value: T) => void
const promise = new Promise<T>((r) => (resolve = r))
return { promise, resolve }
}

export function mintBody(overrides: Record<string, unknown> = {}) {
return {
token: 'workspace-jwt',
permissions: ['workspace:read'],
expires_at: new Date(Date.now() + NINETY_MINUTES_MS).toISOString(),
workspace: { id: 'ws-1', name: 'Personal', type: 'personal' },
role: 'owner',
...overrides
}
}

export function jsonResponse(status: number, body: unknown) {
return new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' }
})
}

export function okFetch(token = 'workspace-jwt') {
return vi.fn<typeof fetch>(async () => jsonResponse(200, mintBody({ token })))
}

export function mintResponse(token: string) {
Comment thread
CodeJuggernaut marked this conversation as resolved.
Resolved
return jsonResponse(200, mintBody({ token }))
}

export function manualIdentity() {
let deliver: ((user: AccountUser | null) => void) | undefined
const unsubscribe = vi.fn()
const port = createTestIdentity<AccountUser>({
onUserChanged: (callback) => {
deliver = callback
return unsubscribe
}
})
return {
port,
fire: (user: AccountUser | null) => deliver?.(user),
unsubscribe
}
}
210 changes: 210 additions & 0 deletions packages/account-core/src/core/credentialCache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import { describe, expect, it, vi } from 'vitest'

import {
credential as makeCredential,
memoryStorage
} from './__fixtures__/sessionFakes.js'
import {
createCredentialCache,
decodeAdopted,
decodeCached,
encodeCached,
selectFreshCredential
} from './credentialCache.js'
import type { AccountCredential } from './sessionContracts.js'

const credential = makeCredential('cached-jwt')

describe('decodeCached', () => {
it.for<{ name: string; raw: string }>([
{ name: 'malformed JSON', raw: '{not json' },
{ name: 'a record missing the contract fields', raw: '{"uid":"uid-1"}' },
{
name: 'a record for another user',
raw: encodeCached({ ...credential, uid: 'someone-else' }, undefined)
},
{
name: 'an empty token',
raw: encodeCached({ ...credential, token: '' }, undefined)
},
{
name: 'a non-finite expiry',
raw: encodeCached(credential, undefined).replace(
'"expiresAt":1000000',
'"expiresAt":1e999'
)
}
])('rejects $name', ({ raw }) => {
expect(decodeCached(raw, 'uid-1')).toBeUndefined()
})

it.for<{ name: string; target: string | undefined }>([
{ name: 'a workspace target', target: 'ws-9' },
{ name: 'the absent personal target', target: undefined }
])('round-trips a credential with $name', ({ target }) => {
expect(decodeCached(encodeCached(credential, target), 'uid-1')).toEqual({
credential,
target
})
})
})

describe('decodeAdopted', () => {
it('returns the credential without the storage-only target', () => {
expect(decodeAdopted({ ...credential, target: 'ws-9' })).toEqual(credential)
})

it.for<{ name: string; message: unknown }>([
{ name: 'a non-object', message: 'not even an object' },
{ name: 'a record missing the contract fields', message: { token: 'x' } },
{
name: 'a non-finite expiry',
message: { ...credential, expiresAt: Number.POSITIVE_INFINITY }
},
{ name: 'an empty token', message: { ...credential, token: '' } }
])('rejects $name', ({ message }) => {
expect(decodeAdopted(message)).toBeUndefined()
})
})

describe('createCredentialCache', () => {
it('writes and reads back a credential for its user only', () => {
const cache = createCredentialCache(memoryStorage())

cache.write(credential, 'ws-9')

expect(cache.read('uid-1')).toEqual({ credential, target: 'ws-9' })
expect(cache.read('someone-else')).toBeUndefined()
})

it('reads a miss after clear', () => {
const cache = createCredentialCache(memoryStorage())
cache.write(credential, undefined)

cache.clear()

expect(cache.read('uid-1')).toBeUndefined()
})

it('degrades a throwing medium to a miss and swallows failed writes and clears', () => {
const cache = createCredentialCache({
read: () => {
throw new Error('blocked')
},
write: () => {
throw new Error('quota')
},
clear: () => {
throw new Error('blocked')
}
})

expect(cache.read('uid-1')).toBeUndefined()
expect(() => cache.write(credential, undefined)).not.toThrow()
expect(() => cache.clear()).not.toThrow()
})
})

describe('selectFreshCredential', () => {
const fresh = makeCredential('fresh')
const stale = makeCredential('stale', { expiresAt: 500_000 })
const now = 400_000
const margin = 300_000

it.for<{
name: string
memory: AccountCredential | undefined
memoryTarget: string | undefined
stored: { credential: AccountCredential; target: string | undefined }
target: string | undefined
expected: string | undefined
}>([
{
name: 'a fresh in-memory credential for the target wins over storage',
memory: fresh,
memoryTarget: 'ws-1',
stored: { credential: makeCredential('stored'), target: 'ws-1' },
target: 'ws-1',
expected: 'fresh'
},
{
name: 'a stale in-memory credential falls through to fresh storage',
memory: stale,
memoryTarget: 'ws-1',
stored: { credential: makeCredential('stored'), target: 'ws-1' },
target: 'ws-1',
expected: 'stored'
},
{
name: 'an in-memory credential for another target is skipped',
memory: fresh,
memoryTarget: 'ws-9',
stored: { credential: makeCredential('stored'), target: 'ws-1' },
target: 'ws-1',
expected: 'stored'
},
{
name: 'a stored credential for another target is skipped',
memory: undefined,
memoryTarget: undefined,
stored: { credential: makeCredential('stored'), target: 'ws-9' },
target: undefined,
expected: undefined
},
{
name: 'an in-memory credential for another user is skipped',
memory: makeCredential('other', { uid: 'uid-2' }),
memoryTarget: 'ws-1',
stored: { credential: stale, target: 'ws-1' },
target: 'ws-1',
expected: undefined
},
{
name: 'nothing fresh anywhere yields no candidate',
memory: stale,
memoryTarget: 'ws-1',
stored: { credential: stale, target: 'ws-1' },
target: 'ws-1',
expected: undefined
}
])('$name', ({ memory, memoryTarget, stored, target, expected }) => {
const selected = selectFreshCredential(
[() => ({ credential: memory, target: memoryTarget }), () => stored],
'uid-1',
target,
now,
margin
)

expect(selected?.token).toBe(expected)
})

it('treats a storage miss as no candidate', () => {
expect(
selectFreshCredential(
[() => ({ credential: undefined, target: undefined }), () => undefined],
'uid-1',
undefined,
now,
margin
)
).toBeUndefined()
})

it('does not consult a later candidate once an earlier one is fresh', () => {
const stored = vi.fn(() => ({
credential: makeCredential('stored'),
target: 'ws-1'
}))

selectFreshCredential(
[() => ({ credential: makeCredential('fresh'), target: 'ws-1' }), stored],
'uid-1',
'ws-1',
400_000,
300_000
)

expect(stored).not.toHaveBeenCalled()
})
})
Loading
Loading