Skip to content

Commit 400a87e

Browse files
chore: apply pr-reviewer fixes for #137
1 parent 22f2bf8 commit 400a87e

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/main/auth.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,32 @@ describe('getAuthStatus', () => {
682682
})
683683
})
684684

685+
it('canonicalizes legacy agentrelay.dev stored tokens before refreshing auth status', async () => {
686+
writeAuthJson(userDataDir, {
687+
accessToken: 'cld_at_legacy_status',
688+
refreshToken: 'cld_rt_legacy_status',
689+
apiUrl: 'https://agentrelay.dev/cloud'
690+
})
691+
mock.fetchMock.mockResolvedValue({
692+
ok: true,
693+
status: 200,
694+
statusText: 'OK',
695+
json: async () => ({ user: { id: 'user-legacy' } })
696+
})
697+
698+
const { getAuthStatus } = await import('./auth')
699+
700+
await expect(getAuthStatus()).resolves.toEqual({
701+
loggedIn: true,
702+
apiUrl: 'https://agentrelay.com/cloud',
703+
user: { username: 'user-legacy' }
704+
})
705+
expect(mock.fetchMock).toHaveBeenCalledWith(
706+
'https://agentrelay.com/cloud/api/v1/auth/whoami',
707+
expect.any(Object)
708+
)
709+
})
710+
685711
it('hydrates a sparse stored profile from whoami', async () => {
686712
writeAuthJson(userDataDir, {
687713
accessToken: 'cld_at_sparse_user',

src/main/auth.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ function loadTokens(): StoredTokens | null {
229229
const decrypted = safeStorage.decryptString(raw)
230230
const parsed = StoredTokensSchema.safeParse(JSON.parse(decrypted))
231231
if (!parsed.success) return null
232-
saveAuthMeta(parsed.data)
233-
return parsed.data
232+
const tokens = { ...parsed.data, apiUrl: normalizeCloudApiUrl(parsed.data.apiUrl) }
233+
saveAuthMeta(tokens)
234+
return tokens
234235
} catch {
235236
return null
236237
}
@@ -602,7 +603,7 @@ function cloudAuthFromStored(tokens: StoredTokens): CloudAuth {
602603
}
603604

604605
function normalizeCloudApiUrl(url: string | undefined): string {
605-
const normalized = (url || getApiUrl()).trim().replace(/\/+$/, '')
606+
const normalized = (url || CLOUD_API_URL).trim().replace(/\/+$/, '')
606607
if (normalized === LEGACY_CLOUD_API_URL) return CLOUD_API_URL
607608
return normalized
608609
}

0 commit comments

Comments
 (0)