Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 46 additions & 0 deletions packages/account/src/core/billing/capabilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ function fakeTransport(answers: BillingResult<BillingHttpResponse>[]) {
return { transport, calls }
}

interface SupersedeContext {
readonly host: ReturnType<typeof fakeSession>
readonly reader: ReturnType<typeof createCapabilitiesReader>
}

describe('createCapabilitiesReader', () => {
it('decodes the server answer and reports the resolved scope', async () => {
const { scopeSource } = fakeSession()
Expand Down Expand Up @@ -577,6 +582,47 @@ describe('createCapabilitiesReader', () => {
expect(result).toEqual({ status: 'error', code: 'REQUEST_FAILED' })
})

it.for([
[
'the workspace changes',
({ host }: SupersedeContext) =>
host.moveTo(
authenticated(
credential({
workspace: { id: 'ws-2', name: 'Team', type: 'team' }
})
)
)
],
[
'the reader is disposed',
({ reader }: SupersedeContext) => reader.dispose()
]
] as const)(
'reports SUPERSEDED when a failure lands after %s',
async ([, supersede]) => {
const host = fakeSession()
let release = () => {}
const gate = new Promise<void>((resolve) => {
release = resolve
})
const transport: BillingTransport = vi.fn(async () => {
await gate
return httpStatus(500)
})
const reader = createCapabilitiesReader({
transport,
scopeSource: host.scopeSource
})

const pending = reader.read()
supersede({ host, reader })
release()

expect(await pending).toEqual({ status: 'error', code: 'SUPERSEDED' })
}
)

it('drops a prior snapshot when the server denies the current actor', async () => {
const { scopeSource } = fakeSession()
const { transport } = fakeTransport([
Expand Down
20 changes: 8 additions & 12 deletions packages/account/src/core/billing/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,19 @@ export function createCapabilitiesReader(
// waiting on it. Each caller's signal releases only that caller, below.
const promise = (async (): Promise<BillingResult<CapabilitiesSnapshot>> => {
const result = await requestCapabilities(scope)
if (result.status !== 'ok') {
if (
result.code === 'ACCESS_DENIED' &&
scopeTracker.isCurrent(context)
) {
snapshot = undefined
}
return result
}

// The publish guard. Between issuing the request and settling it the
// host may have changed workspace or signed out, and a snapshot cached
// now would be attributed to whoever is signed in next.
// host may have changed workspace or signed out, so a snapshot cached
// now would be attributed to whoever is signed in next, and a failure
// says nothing about the scope that has since taken over.
if (lifetime.disposed || !scopeTracker.isCurrent(context)) {
return { status: 'error', code: 'SUPERSEDED' }
}

if (result.status !== 'ok') {
if (result.code === 'ACCESS_DENIED') snapshot = undefined
return result
}

// A mutation committed while this read was in flight, and the revision
// the read returns cannot rule out having missed it: the server mints
// that number when it serializes, not when it read. The value is still
Expand Down
46 changes: 46 additions & 0 deletions packages/account/src/core/billing/credits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ function fakeTransport(answers: BillingResult<BillingHttpResponse>[]) {
return { transport, calls }
}

interface SupersedeContext {
readonly host: ReturnType<typeof fakeSession>
readonly reader: ReturnType<typeof createCreditsReader>
}

describe('createCreditsReader', () => {
it('reads the balance from the billing balance route', async () => {
const { scopeSource } = fakeSession()
Expand Down Expand Up @@ -395,6 +400,47 @@ describe('createCreditsReader', () => {
expect(reader.getSnapshot()?.balance.amount_micros).toBe(12_500_000)
})

it.for([
[
'the workspace changes',
({ host }: SupersedeContext) =>
host.moveTo(
authenticated(
credential({
workspace: { id: 'ws-2', name: 'Team', type: 'team' }
})
)
)
],
[
'the reader is disposed',
({ reader }: SupersedeContext) => reader.dispose()
]
] as const)(
'reports SUPERSEDED when a failure lands after %s',
async ([, supersede]) => {
const host = fakeSession()
let release = () => {}
const gate = new Promise<void>((resolve) => {
release = resolve
})
const transport: BillingTransport = vi.fn(async () => {
await gate
return httpStatus(500)
})
const reader = createCreditsReader({
transport,
scopeSource: host.scopeSource
})

const pending = reader.read()
supersede({ host, reader })
release()

expect(await pending).toEqual({ status: 'error', code: 'SUPERSEDED' })
}
)

it('drops the last good balance when a later read is denied', async () => {
const { scopeSource } = fakeSession()
const { transport } = fakeTransport([httpOk(BALANCE), httpStatus(401)])
Expand Down
19 changes: 7 additions & 12 deletions packages/account/src/core/billing/credits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,18 @@ export function createCreditsReader(
context,
promise: (async () => {
const result = await requestBalance(scope)
if (result.status !== 'ok') {
if (
result.code === 'ACCESS_DENIED' &&
scopeTracker.isCurrent(context)
) {
snapshot = undefined
}
return result
}

// The publish guard: a balance that arrives after the host moved to
// another workspace or signed out belongs to neither, and showing it
// would state one account's credits under another's name.
// another workspace or signed out belongs to neither, and a failure
// says nothing about the scope that has since taken over.
if (lifetime.disposed || !scopeTracker.isCurrent(context)) {
return { status: 'error', code: 'SUPERSEDED' }
}

if (result.status !== 'ok') {
if (result.code === 'ACCESS_DENIED') snapshot = undefined
return result
}

snapshot = result.value
return result
})()
Expand Down
46 changes: 46 additions & 0 deletions packages/account/src/core/billing/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ function fakeTransport(answers: BillingResult<BillingHttpResponse>[]) {
return { transport, calls }
}

interface SupersedeContext {
readonly host: ReturnType<typeof fakeSession>
readonly reader: ReturnType<typeof createBillingStatusReader>
}

describe('createBillingStatusReader', () => {
it('reads and validates the authoritative billing status', async () => {
const { scopeSource } = fakeSession()
Expand Down Expand Up @@ -271,6 +276,47 @@ describe('createBillingStatusReader', () => {
expect(reader.getSnapshot()).toBeUndefined()
})

it.for([
[
'the workspace changes',
({ host }: SupersedeContext) =>
host.moveTo(
authenticated(
credential({
workspace: { id: 'ws-2', name: 'Team', type: 'team' }
})
)
)
],
[
'the reader is disposed',
({ reader }: SupersedeContext) => reader.dispose()
]
] as const)(
'reports SUPERSEDED when a failure lands after %s',
async ([, supersede]) => {
const host = fakeSession()
let release = () => {}
const gate = new Promise<void>((resolve) => {
release = resolve
})
const transport: BillingTransport = vi.fn(async () => {
await gate
return httpStatus(500)
})
const reader = createBillingStatusReader({
transport,
scopeSource: host.scopeSource
})

const pending = reader.read()
supersede({ host, reader })
release()

expect(await pending).toEqual({ status: 'error', code: 'SUPERSEDED' })
}
)

it('normalizes a throwing host transport to a coded failure', async () => {
const { scopeSource } = fakeSession()
const transport: BillingTransport = vi.fn(() => {
Expand Down
18 changes: 8 additions & 10 deletions packages/account/src/core/billing/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,18 @@ export function createBillingStatusReader(
context,
promise: (async () => {
const result = await requestStatus(scope)
if (result.status !== 'ok') {
if (
result.code === 'ACCESS_DENIED' &&
scopeTracker.isCurrent(context)
) {
snapshot = undefined
}
return result
}

// A status that arrives after the host moved to another workspace
// describes neither, and a failure says nothing about the scope that
// has since taken over.
if (lifetime.disposed || !scopeTracker.isCurrent(context)) {
return { status: 'error', code: 'SUPERSEDED' }
}

if (result.status !== 'ok') {
if (result.code === 'ACCESS_DENIED') snapshot = undefined
return result
}

snapshot = result.value
return result
})()
Expand Down
Loading