diff --git a/src/main/orca-profiles/project-transfer/profile-project-transfer.test.ts b/src/main/orca-profiles/project-transfer/profile-project-transfer.test.ts index 823124fa78d..7e5a8a6ae94 100644 --- a/src/main/orca-profiles/project-transfer/profile-project-transfer.test.ts +++ b/src/main/orca-profiles/project-transfer/profile-project-transfer.test.ts @@ -66,6 +66,15 @@ async function loadTransferModule() { return import('./profile-project-transfer') } +function transferMove( + transfer: Awaited>['transferOrcaProfileProject'] +) { + return transfer( + { sourceProfileId: 'personal', targetProfileId: 'work', repoId: 'repo-1', mode: 'move' }, + testState.dir + ) +} + function profile(id: string, name: string): OrcaProfileIndex['profiles'][number] { return { id, @@ -135,6 +144,22 @@ function makeState(overrides: Partial = {}): PersistedState { } } +// A profile saved before artifactSharingEnabled existed carries no defaults for it. +function writeOlderProfileStateDatabase( + profileId: string, + settings: Record, + repos: Repo[] = [] +): void { + const databasePath = profileDatabasePath(profileId) + mkdirSync(join(databasePath, '..'), { recursive: true }) + const opened = openProfileStateDatabase(databasePath, profileId) + try { + importProfileStateJson(opened.db, JSON.stringify({ settings, repos })) + } finally { + opened.db.close() + } +} + describe('profile project transfer', () => { beforeEach(() => { testState.dir = mkdtempSync(join(tmpdir(), 'orca-profile-transfer-')) @@ -200,4 +225,26 @@ describe('profile project transfer', () => { expect(readProfileStateDatabase('personal').repos).toHaveLength(1) expect(existsSync(intentPath)).toBe(true) }) + + it('moves a project without granting publishing permission to either older profile', async () => { + writeOlderProfileStateDatabase('personal', {}, [makeRepo()]) + writeOlderProfileStateDatabase('work', {}) + const { transferOrcaProfileProject } = await loadTransferModule() + + expect(transferMove(transferOrcaProfileProject)).toMatchObject({ status: 'transferred' }) + expect(readProfileStateDatabase('personal').settings).not.toHaveProperty( + 'artifactSharingEnabled' + ) + expect(readProfileStateDatabase('work').settings).not.toHaveProperty('artifactSharingEnabled') + }) + + it('keeps explicit publishing choices when moving a project', async () => { + writeOlderProfileStateDatabase('personal', { artifactSharingEnabled: true }, [makeRepo()]) + writeOlderProfileStateDatabase('work', { artifactSharingEnabled: false }) + const { transferOrcaProfileProject } = await loadTransferModule() + + expect(transferMove(transferOrcaProfileProject)).toMatchObject({ status: 'transferred' }) + expect(readProfileStateDatabase('personal').settings.artifactSharingEnabled).toBe(true) + expect(readProfileStateDatabase('work').settings.artifactSharingEnabled).toBe(false) + }) }) diff --git a/src/main/persistence/applying-settings/persistence-artifact-sharing-default.test.ts b/src/main/persistence/applying-settings/persistence-artifact-sharing-default.test.ts new file mode 100644 index 00000000000..7a0f2285106 --- /dev/null +++ b/src/main/persistence/applying-settings/persistence-artifact-sharing-default.test.ts @@ -0,0 +1,59 @@ +import { closeTestStores, testState, createStore, writeDataFile } from '../persistence-test-harness' +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' +import { rmSync, mkdtempSync } from 'node:fs' +import { join } from 'node:path' +import { tmpdir } from 'node:os' + +vi.mock('electron', () => ({ + app: { getPath: () => testState.dir }, + safeStorage: { + isEncryptionAvailable: () => true, + encryptString: (plaintext: string) => Buffer.from(`encrypted:${plaintext}`, 'utf-8'), + decryptString: (ciphertext: Buffer) => ciphertext.toString('utf-8').slice('encrypted:'.length) + } +})) +vi.mock('../../telemetry/client', () => ({ track: vi.fn() })) +vi.mock('../../telemetry/cohort-classifier', () => ({ + getCohortAtEmit: () => ({ nth_repo_added: 2 }) +})) + +function writeSettings(settings: Record): void { + writeDataFile({ + schemaVersion: 1, + repos: [], + worktreeMeta: {}, + settings, + ui: {}, + githubCache: { pr: {}, issue: {} }, + workspaceSession: {} + }) +} + +describe('artifact sharing default', () => { + beforeEach(() => { + testState.dir = mkdtempSync(join(tmpdir(), 'orca-test-')) + }) + + afterEach(async () => { + await closeTestStores() + rmSync(testState.dir, { recursive: true, force: true }) + }) + + it('allows publishing on a new profile and keeps it after a reload', async () => { + const store = createStore() + expect(store.getSettings().artifactSharingEnabled).toBe(true) + store.flush() + await closeTestStores() + expect(createStore().getSettings().artifactSharingEnabled).toBe(true) + }) + + it('keeps a stored opt-out', () => { + writeSettings({ artifactSharingEnabled: false }) + expect(createStore().getSettings().artifactSharingEnabled).toBe(false) + }) + + it('keeps publishing off for a stored profile saved before the setting existed', () => { + writeSettings({ showArtifactsButton: false }) + expect(createStore().getSettings().artifactSharingEnabled).toBe(false) + }) +}) diff --git a/src/renderer/src/web/preload-api/web-artifact-sharing-setting.test.ts b/src/renderer/src/web/preload-api/web-artifact-sharing-setting.test.ts new file mode 100644 index 00000000000..90bd76157a2 --- /dev/null +++ b/src/renderer/src/web/preload-api/web-artifact-sharing-setting.test.ts @@ -0,0 +1,18 @@ +// @vitest-environment happy-dom +import { beforeEach, expect, it, vi } from 'vitest' +import { getStoredSettings } from './web-preferences-store' +import { SETTINGS_STORAGE_KEY } from './web-storage' + +vi.mock('./web-runtime-session', () => ({ + webRuntimeState: { activeEnvironment: null }, + requireActiveEnvironmentOrNull: () => null +})) + +beforeEach(() => localStorage.clear()) + +it('reports publishing off until the host grant has been mirrored', () => { + expect(getStoredSettings().artifactSharingEnabled).toBe(false) + + localStorage.setItem(SETTINGS_STORAGE_KEY, JSON.stringify({ artifactSharingEnabled: true })) + expect(getStoredSettings().artifactSharingEnabled).toBe(true) +})