Files
archived-orca/src/main/cloud-sync/cloud-sync-service.test.ts
T
Neil 23ff65d436 refactor(cloud): delete "Mirror to this computer" (YAGNI); keep .context sync
Neil: Mirror goes entirely, not behind a flag. The cloud → this computer sync engine served both
`.context` (plans/screenshots, on by default) and the whole-workspace mirror; it now serves only
`.context`.
- Contract/IPC: choice is { context }; setChoice takes only context. Old saved records with
  `mirror: true` still load (the field is ignored).
- Main: removed the whole-workspace listing, mirror limits, mirror-only local cleanup and the
  remote filter call; the watch keeps only .context paths. Files outside .context left by an older
  build are removed on the next pass (tested).
- Renderer: removed the "Mirror to this computer…" menu row and its focus plumbing, the mirror
  switch in the card's "On this computer" panel, and the "Mirroring" card statuses. The panel's
  folder line (Open folder / Copy path) now shows for .context.
- Strings: deleted orcaCloudSync.menuItem / mirrorLabel / mirrorHelper and the card's mirroring
  statuses; the over-limit line names .context. Other locales had no mirror keys.
- Tests/fixtures updated; mirror-only ones deleted.
#824 (Mirror redesign) is closed and its branch deleted.
2026-11-16 01:50:20 -08:00

169 lines
5.7 KiB
TypeScript

import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { CloudSyncView } from '../../shared/cloud-sync/cloud-sync-contract'
import type { EphemeralVmRuntimeRecord } from '../../shared/ephemeral-vm-runtimes'
import { createFakeVm } from './cloud-sync-fake-vm.test-support'
import { createCloudSyncService } from './cloud-sync-service'
import { createCloudSyncStore } from './cloud-sync-store'
const dirs: string[] = []
afterEach(async () => {
await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })))
})
function runtime(status: EphemeralVmRuntimeRecord['status']): EphemeralVmRuntimeRecord {
return {
id: 'rt-1',
recipeId: 'orca-cloud',
recipe: { id: 'orca-cloud', name: 'Orca Cloud', create: 'x', provider: 'orca-cloud' },
repoPath: '/Users/me/src/Hello-World',
workspaceId: 'ws',
workspaceName: 'my-ws',
runtimeEnvironmentId: 'env-1',
status,
cleanupStatus: 'not_started',
createdAt: 1,
updatedAt: 1
}
}
async function setup() {
const home = await mkdtemp(join(tmpdir(), 'cloud-sync-home-'))
const userData = await mkdtemp(join(tmpdir(), 'cloud-sync-ud-'))
dirs.push(home, userData)
const vm = createFakeVm()
vm.write('.context/plan.md', 'plan')
vm.write('src/a.ts', 'a')
let record = runtime('running')
let archived = false
const views: CloudSyncView[] = []
const opened: string[] = []
const service = createCloudSyncService({
store: createCloudSyncStore(userData),
listRuntimes: () => [record],
callFor: () => vm.call,
watchFor: () => vm.subscribe,
isArchived: async () => archived,
broadcast: (view) => views.push(view),
openPath: async (path) => {
opened.push(path)
return true
},
home,
log: () => undefined
})
const folder = join(home, 'orca', 'cloud', 'Hello-World', 'my-ws')
const synced = async (): Promise<void> => {
await vi.waitFor(() => expect(views.at(-1)?.phase).toBe('synced'))
}
return {
vm,
service,
userData,
folder,
views,
opened,
synced,
setStatus: (status: EphemeralVmRuntimeRecord['status']) => {
record = runtime(status)
},
setArchived: (value: boolean) => {
archived = value
}
}
}
describe('cloud sync service', () => {
it('syncs .context by default into ~/orca/cloud/<repository>/<workspace>', async () => {
const t = await setup()
await t.service.reconcile()
await t.synced()
expect(await readFile(join(t.folder, '.context/plan.md'), 'utf8')).toBe('plan')
const view = await t.service.getState('ws')
expect(view).toMatchObject({
choice: { context: true },
displayPath: '~/orca/cloud/Hello-World/my-ws/.context'
})
await t.service.openFolder('ws')
expect(t.opened).toEqual([join(t.folder, '.context')])
await t.service.dispose()
})
it('loads a choice saved by an older build that also had a mirror field', async () => {
const t = await setup()
await mkdir(join(t.userData, 'orca-cloud-sync'), { recursive: true })
await writeFile(
join(t.userData, 'orca-cloud-sync', 'choices.json'),
JSON.stringify({ version: 1, workspaces: { ws: { context: false, mirror: true } } })
)
expect(await t.service.getState('ws')).toMatchObject({
choice: { context: false },
phase: 'off'
})
const view = await t.service.setChoice('ws', { context: true })
expect(view?.choice).toEqual({ context: true })
await t.synced()
expect(await readFile(join(t.folder, '.context/plan.md'), 'utf8')).toBe('plan')
await t.service.dispose()
})
it('asleep: stops, keeps the copy and reports waiting with the last sync', async () => {
const t = await setup()
await t.service.reconcile()
await t.synced()
t.setStatus('suspended')
await t.service.reconcile()
expect(t.views.at(-1)).toMatchObject({ phase: 'waiting' })
expect(t.views.at(-1)?.lastSyncedAt).not.toBeNull()
expect(t.vm.openWatches()).toBe(0)
expect(await readFile(join(t.folder, '.context/plan.md'), 'utf8')).toBe('plan')
await t.service.dispose()
})
it('archived: removes the copy it made but not files someone added', async () => {
const t = await setup()
await t.service.reconcile()
await t.synced()
await writeFile(join(t.folder, '.context/mine.txt'), 'mine')
expect(await t.service.removalPreview('ws')).toEqual({ hasLocalCopy: true, editedHere: 0 })
t.setStatus('suspended')
await t.service.noteArchived('ws')
await expect(stat(join(t.folder, '.context/plan.md'))).rejects.toThrow()
expect(await readFile(join(t.folder, '.context/mine.txt'), 'utf8')).toBe('mine')
await t.service.dispose()
})
it('archived elsewhere is found on the slow check while asleep', async () => {
const t = await setup()
await t.service.reconcile()
await t.synced()
t.setStatus('suspended')
t.setArchived(true)
await t.service.reconcile()
await expect(stat(t.folder)).rejects.toThrow()
await t.service.dispose()
})
it('deleted: the whole folder goes', async () => {
const t = await setup()
await t.service.reconcile()
await t.synced()
t.setStatus('cleaned')
await t.service.reconcile()
await expect(stat(t.folder)).rejects.toThrow()
await t.service.dispose()
})
it('turning sync off removes the copy', async () => {
const t = await setup()
await t.service.reconcile()
await t.synced()
const view = await t.service.setChoice('ws', { context: false })
expect(view?.phase).toBe('off')
await expect(stat(join(t.folder, '.context/plan.md'))).rejects.toThrow()
await t.service.dispose()
})
})