Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 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
57 changes: 57 additions & 0 deletions src/main/runtime/relay/desktop-relay-service-failure-cause.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it } from 'vitest'
import type { RelayOfflineReason } from './relay-offline-reason'
import { DesktopRelayService } from './desktop-relay-service'

// Why Object.create + Object.assign: mirrors the constructor-bypassing setup
// the existing 'liveness safety net lifecycle' test uses to fake the private
// coordinator field without standing up a real broker/runtimeRpc.
function serviceWithOfflineReason(offlineReason: RelayOfflineReason | null): {
requireActiveBroker: () => Promise<unknown>
} {
const coordinator = {
getActiveBroker: () => null,
getLiveBroker: () => null,
waitForLiveBrokerResult: async () => ({ broker: null, offlineReason })
}
const service = Object.create(DesktopRelayService.prototype) as DesktopRelayService
Object.assign(service, { coordinator })
return service as unknown as { requireActiveBroker: () => Promise<unknown> }
}

describe('DesktopRelayService.requireActiveBroker failure cause codes', () => {
it('names a signed-out session instead of the generic control-not-active code', async () => {
await expect(serviceWithOfflineReason('signed-out').requireActiveBroker()).rejects.toThrow(
'relay_signed_out'
)
})

it('names a missing entitlement instead of the generic control-not-active code', async () => {
await expect(serviceWithOfflineReason('not_entitled').requireActiveBroker()).rejects.toThrow(
'relay_not_entitled'
)
})

it('names a broker that never came up instead of the generic control-not-active code', async () => {
await expect(
serviceWithOfflineReason('broker_unavailable').requireActiveBroker()
).rejects.toThrow('relay_broker_unavailable')
})

it('names a relay that rejected this desktop instead of the generic control-not-active code', async () => {
await expect(serviceWithOfflineReason('broker_rejected').requireActiveBroker()).rejects.toThrow(
'relay_broker_rejected'
)
})

it('names a cloud session that could not be read instead of blaming the relay', async () => {
await expect(
serviceWithOfflineReason('auth_unavailable').requireActiveBroker()
).rejects.toThrow('relay_auth_unavailable')
})

it('falls back to relay_control_not_active when no cause was recorded', async () => {
await expect(serviceWithOfflineReason(null).requireActiveBroker()).rejects.toThrow(
'relay_control_not_active'
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { describe, expect, it, vi } from 'vitest'
import { DesktopRelayService } from './desktop-relay-service'
import type { MobilePairingConnectionMode } from '../../../shared/mobile-pairing-connection-mode'

/**
* A host LAN flip lands mid-mint.
*
* `hasDemand` filters the in-flight operation's OWN transient ref through the live policy
* (relay-demand-ledger.ts:50-54), so the flip withdraws the demand that operation is holding.
* The coordinator then publishes `standby`, which clears `offlineReason` to null, and the broker
* wait returns with no cause — `relay_control_not_active`, the generic code this stack exists to
* remove. The flip is the cause and `relay_disabled_for_device` is already its word.
*/
type TransientDemandHost = {
withTransientDemand: (
kind: string,
deviceId: string,
operation: () => Promise<unknown>
) => Promise<unknown>
}

function serviceWithMode(mode: { current: MobilePairingConnectionMode }): {
run: (operation: () => Promise<unknown>) => Promise<unknown>
release: ReturnType<typeof vi.fn>
} {
const release = vi.fn()
const service = Object.create(DesktopRelayService.prototype) as DesktopRelayService
Object.assign(service, {
hostMobilePairingConnectionMode: () => mode.current,
runtimeRpc: {
getDeviceRegistry: () => ({ getMobilePairingConnectionMode: () => 'automatic' })
},
demandLedger: { acquireTransient: () => release },
refreshDemand: () => {}
})
const host = service as unknown as TransientDemandHost
return {
run: (operation) => host.withTransientDemand.call(service, 'pairing', 'device-1', operation),
release
}
}

describe('DesktopRelayService policy flip during an in-flight grant', () => {
it('names the LAN flip rather than the generic control-not-active code', async () => {
const mode = { current: 'automatic' as MobilePairingConnectionMode }
const { run, release } = serviceWithMode(mode)

await expect(
run(async () => {
mode.current = 'local-only'
// What requireActiveBroker throws once the flip cleared the offline reason.
throw new Error('relay_control_not_active')
})
).rejects.toThrow('relay_disabled_for_device')
expect(release).toHaveBeenCalled()
})

it('does not rewrite a failure the policy had nothing to do with', async () => {
const mode = { current: 'automatic' as MobilePairingConnectionMode }
const { run } = serviceWithMode(mode)

await expect(
run(async () => {
throw new Error('relay_broker_unavailable')
})
).rejects.toThrow('relay_broker_unavailable')
})

it('still refuses at the entry gate when the policy already excludes the device', async () => {
const mode = { current: 'local-only' as MobilePairingConnectionMode }
const { run } = serviceWithMode(mode)
const operation = vi.fn(async () => 'unreachable')

await expect(run(operation)).rejects.toThrow('relay_disabled_for_device')
expect(operation).not.toHaveBeenCalled()
})

it('leaves a successful grant alone even if the policy flips under it', async () => {
const mode = { current: 'automatic' as MobilePairingConnectionMode }
const { run } = serviceWithMode(mode)

await expect(
run(async () => {
mode.current = 'local-only'
return 'minted'
})
).resolves.toBe('minted')
})
})
68 changes: 68 additions & 0 deletions src/main/runtime/relay/desktop-relay-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,74 @@ describe('liveness safety net lifecycle', () => {
})
})

describe('host LAN policy applies to already-paired devices (#18211)', () => {
function serviceUnderPolicy(
hostMode: () => 'automatic' | 'local-only',
deviceMode: 'automatic' | 'local-only' = 'automatic'
) {
const registry = {
getDevice: () => ({ deviceId: 'device-1', scope: 'mobile' }),
getMobilePairingConnectionMode: () => deviceMode
}
const coordinator = { reconcile: vi.fn(), stop: vi.fn() }
const service = Object.create(DesktopRelayService.prototype) as DesktopRelayService
Object.assign(service, {
coordinator,
demandLedger: { nextPendingExpiry: () => null, acquireTransient: () => () => {} },
hostMobilePairingConnectionMode: hostMode,
stopped: false,
livenessTimer: null,
demandExpiryTimer: null
})
Object.defineProperty(service, 'runtimeRpc', {
value: { getDeviceRegistry: () => registry }
})
return { service, coordinator }
}

afterEach(() => vi.useRealTimers())

it('refuses every relay grant for an automatic device once the host picks LAN', async () => {
vi.useFakeTimers()
const { service } = serviceUnderPolicy(() => 'local-only')
await expect(service.getEndpoints(context({ transport: 'direct' }), {})).resolves.toEqual({
v: 1,
relay: null
})
await expect(
service.provisionRelay(context({ transport: 'direct' }), {
reqId: 'install-1',
newResumeTokenHash: 'A'.repeat(43)
})
).rejects.toThrow('relay_disabled_for_device')
// Why: createPairingRelay had no per-device gate at all; the choke point
// in withTransientDemand is what covers it.
await expect(service.createPairingRelay('device-1')).rejects.toThrow(
'relay_disabled_for_device'
)
service.stop()
})

it('never grants Relay to a local-only device even when the host allows it', async () => {
vi.useFakeTimers()
const { service } = serviceUnderPolicy(() => 'automatic', 'local-only')
await expect(service.createPairingRelay('device-1')).rejects.toThrow(
'relay_disabled_for_device'
)
service.stop()
})

it('pairingPolicyChanged reconciles without the pairing-churn linger', () => {
vi.useFakeTimers()
const { service, coordinator } = serviceUnderPolicy(() => 'local-only')
service.pairingPolicyChanged()
expect(coordinator.reconcile).toHaveBeenLastCalledWith({ skipLinger: true })
service.demandStateChanged()
expect(coordinator.reconcile).toHaveBeenLastCalledWith(undefined)
service.stop()
})
})

describe('local-only mobile pairing', () => {
it('refuses endpoint discovery and provisioning without opening Relay demand', async () => {
const registry = {
Expand Down
Loading