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
51 changes: 51 additions & 0 deletions src/lib/installer-core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type {
InstallerMachineContext,
BranchCheckOutput,
} from './installer-core.types.js';
import type { EnvFileInfo } from './credential-discovery.js';
import type { StagingCredentials } from './staging-api.js';

// Shared mock actors for reuse across tests
const baseMockActors = {
Expand Down Expand Up @@ -378,5 +380,54 @@ describe('InstallerCore State Machine', () => {
expect(actor.getSnapshot().value).toBe('complete');
actor.stop();
});

it('skips device auth when checkStoredAuth returns true (unclaimed env)', async () => {
const emitter = createInstallerEventEmitter();
const options: InstallerOptions = {
debug: false,
forceInstall: false,
installDir: '/test/project',
default: false,
local: true,
ci: false,
skipAuth: true,
dashboard: false,
emitter,
// No CLI credentials — forces credential gathering flow
};

let deviceAuthStarted = false;

const machine = installerMachine.provide({
actors: {
...baseMockActors,
detectEnvFiles: fromPromise<EnvFileInfo, { installDir: string }>(async () => ({
found: false,
})),
checkStoredAuth: fromPromise<boolean, void>(async () => true),
runDeviceAuth: fromPromise(async () => {
deviceAuthStarted = true;
throw new Error('device auth should not be called');
}),
fetchStagingCredentials: fromPromise<StagingCredentials, void>(async () => ({
clientId: 'client_unclaimed',
apiKey: 'sk_test_unclaimed',
})),
},
});

const actor = createActor(machine, {
input: { emitter, options },
});

actor.start();
actor.send({ type: 'START' });

await new Promise((r) => setTimeout(r, 200));

expect(deviceAuthStarted).toBe(false);
expect(actor.getSnapshot().value).toBe('complete');
actor.stop();
});
});
});
7 changes: 6 additions & 1 deletion src/lib/run-with-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
getStagingCredentials,
saveStagingCredentials,
} from './credentials.js';
import { getConfig, saveConfig, getActiveEnvironment } from './config-store.js';
import { getConfig, saveConfig, getActiveEnvironment, isUnclaimedEnvironment } from './config-store.js';
import { checkForEnvFiles, discoverCredentials } from './credential-discovery.js';
import { requestDeviceCode, pollForToken } from './device-auth.js';
import { fetchStagingCredentials as fetchStagingCredentialsApi } from './staging-api.js';
Expand Down Expand Up @@ -321,6 +321,11 @@ export async function runWithCore(options: InstallerOptions): Promise<void> {
}),

checkStoredAuth: fromPromise(async () => {
const activeEnv = getActiveEnvironment();
if (activeEnv?.apiKey && isUnclaimedEnvironment(activeEnv)) {
return true;
}

const token = getAccessToken();
return token !== null;
}),
Expand Down
Loading