This repository was archived by the owner on Jul 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix switching users on client, properly changes stdiod config #40
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' | ||
|
|
||
| // Controller side effects are mocked so the tests never touch launchctl or | ||
| // write config.toml. Each mock is a spy we assert against. | ||
| let launchAgentLoaded = true | ||
| const uninstallMock = vi.fn(async (..._args: unknown[]) => ({ ok: true }) as const) | ||
| const resetMock = vi.fn(async (..._args: unknown[]) => ({ ok: true }) as const) | ||
|
|
||
| vi.mock('../stdiod/controller', () => ({ | ||
| isLaunchAgentLoaded: async () => launchAgentLoaded, | ||
| uninstall: (...args: unknown[]) => uninstallMock(...(args as [])), | ||
| resetStdiod: (...args: unknown[]) => resetMock(...(args as [])) | ||
| })) | ||
|
|
||
| vi.mock('../stdiod/stdiodLog', () => ({ stdiodLog: () => {} })) | ||
|
|
||
| // Active-account credentials. Overridden per test. | ||
| let apiBaseUrl: string | null = 'https://api.testedison.example' | ||
| let creds: { apiKey: string; edisonSecretKey?: string } | null = { | ||
| apiKey: 'test-key', | ||
| edisonSecretKey: 'test-secret' | ||
| } | ||
|
|
||
| vi.mock('../infra/setupConfig', () => ({ | ||
| getApiBaseUrl: () => apiBaseUrl, | ||
| getCredentialsForEnv: () => creds | ||
| })) | ||
|
|
||
| import { | ||
| reprovisionStdiodForActiveAccount, | ||
| teardownStdiodForSignOut | ||
| } from '../stdiod/accountSwitch' | ||
|
|
||
| describe('teardownStdiodForSignOut', () => { | ||
| beforeEach(() => { | ||
| uninstallMock.mockClear() | ||
| resetMock.mockClear() | ||
| delete process.env.EDISON_DRY_RUN | ||
| }) | ||
|
|
||
| it('unloads the daemon but keeps config.toml (purge:false)', async () => { | ||
| await teardownStdiodForSignOut() | ||
| expect(uninstallMock).toHaveBeenCalledTimes(1) | ||
| expect(uninstallMock).toHaveBeenCalledWith({ purge: false }) | ||
| expect(resetMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('swallows uninstall failures so sign-out never blocks', async () => { | ||
| uninstallMock.mockRejectedValueOnce(new Error('launchctl exploded')) | ||
| await expect(teardownStdiodForSignOut()).resolves.toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('reprovisionStdiodForActiveAccount', () => { | ||
| beforeEach(() => { | ||
| uninstallMock.mockClear() | ||
| resetMock.mockClear() | ||
| launchAgentLoaded = true | ||
| apiBaseUrl = 'https://api.testedison.example' | ||
| creds = { apiKey: 'test-key', edisonSecretKey: 'test-secret' } | ||
| delete process.env.EDISON_DRY_RUN | ||
| }) | ||
| afterEach(() => { | ||
| delete process.env.EDISON_DRY_RUN | ||
| }) | ||
|
|
||
| it('does nothing when the daemon is not installed', async () => { | ||
| launchAgentLoaded = false | ||
| await reprovisionStdiodForActiveAccount() | ||
| expect(resetMock).not.toHaveBeenCalled() | ||
| expect(uninstallMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('resets the daemon onto the active account when installed', async () => { | ||
| await reprovisionStdiodForActiveAccount() | ||
| expect(resetMock).toHaveBeenCalledTimes(1) | ||
| expect(resetMock).toHaveBeenCalledWith({ | ||
| backend: 'https://api.testedison.example', | ||
| apiKey: 'test-key', | ||
| edisonSecretKey: 'test-secret' | ||
| }) | ||
| expect(uninstallMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('stops the daemon when installed but the active account has no credentials', async () => { | ||
| creds = null | ||
| await reprovisionStdiodForActiveAccount() | ||
| expect(resetMock).not.toHaveBeenCalled() | ||
| expect(uninstallMock).toHaveBeenCalledWith({ purge: false }) | ||
| }) | ||
|
|
||
| it('never re-points onto stale credentials when the backend url is missing', async () => { | ||
| apiBaseUrl = null | ||
| await reprovisionStdiodForActiveAccount() | ||
| expect(resetMock).not.toHaveBeenCalled() | ||
| expect(uninstallMock).toHaveBeenCalledWith({ purge: false }) | ||
| }) | ||
|
|
||
| it('is a no-op under EDISON_DRY_RUN (no launchctl probe in e2e)', async () => { | ||
| process.env.EDISON_DRY_RUN = '1' | ||
| await reprovisionStdiodForActiveAccount() | ||
| expect(resetMock).not.toHaveBeenCalled() | ||
| expect(uninstallMock).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Keep the daemon bound to the active account on sign-out and account switch. | ||
| // config.toml only changes on `login`, so otherwise the daemon stays on the | ||
| // previous account. | ||
|
|
||
| import { getApiBaseUrl, getCredentialsForEnv } from '../infra/setupConfig' | ||
|
|
||
| import { isLaunchAgentLoaded, resetStdiod, uninstall } from './controller' | ||
| import { stdiodLog } from './stdiodLog' | ||
|
|
||
| // Stop the daemon on sign-out. purge=false keeps config.toml for a quick | ||
| // re-enable; unloading the unit is what stops the old tunnel. | ||
| export async function teardownStdiodForSignOut(): Promise<void> { | ||
| try { | ||
| await uninstall({ purge: false }) | ||
| } catch (err) { | ||
| stdiodLog(`sign-out teardown failed: ${err instanceof Error ? err.message : String(err)}`) | ||
| } | ||
| } | ||
|
|
||
| // On account switch: reset an installed daemon onto the new account (keeping | ||
| // its device identity), leave it off if it was off, stop it if there are no | ||
| // credentials. | ||
| export async function reprovisionStdiodForActiveAccount(): Promise<void> { | ||
| // Don't probe launchctl in tests. | ||
| if (process.env.EDISON_DRY_RUN === '1') return | ||
|
|
||
| let installed = false | ||
| try { | ||
| installed = await isLaunchAgentLoaded() | ||
| } catch { | ||
| installed = false | ||
| } | ||
| if (!installed) return | ||
|
|
||
| const backend = getApiBaseUrl() | ||
| const creds = getCredentialsForEnv() | ||
| if (!backend || !creds?.apiKey) { | ||
| stdiodLog('account switch: no credentials for the active account; stopping the daemon') | ||
| await teardownStdiodForSignOut() | ||
| return | ||
| } | ||
|
|
||
| stdiodLog('account switch: re-pointing the daemon at the active account') | ||
| try { | ||
| const result = await resetStdiod({ | ||
| backend, | ||
| apiKey: creds.apiKey, | ||
| edisonSecretKey: creds.edisonSecretKey | ||
| }) | ||
| if (!result.ok) { | ||
|
dimitriosGX marked this conversation as resolved.
|
||
| stdiodLog( | ||
| `account switch: reprovision failed: ${result.errorMessage ?? result.errorCode ?? 'unknown'}` | ||
| ) | ||
| } | ||
| } catch (err) { | ||
| stdiodLog( | ||
| `account switch: reprovision threw: ${err instanceof Error ? err.message : String(err)}` | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.