Files
orca/src/main/project-runtime-git-options.test.ts
T
bfbdcd7e80 feat(wsl): run Git for Windows for a WSL project on a Windows drive (D1c-2) (#33090)
* feat(wsl): run Git for Windows for a WSL project on a Windows drive

A project on C:\ whose runtime is WSL ran every Orca Git command through wsl.exe on
/mnt/c: ~300x slower than Windows Git, and blind to Windows-side edits. The project's
WSL runtime preference gains an optional `git: 'windows-host'`. With it, Orca's Git
(and where new worktrees go) is Windows; terminals, agents, accounts and hooks still
resolve to WSL, so nothing else changes. Older builds drop the field and go back to WSL
Git.

The conversion checks first, then the caller saves the returned preference through the
normal project update:
- Git for Windows must exist.
- New worktrees must not be set to go inside the distro.
- No linked worktree may have a Linux-path link. WSL Git made those; Windows Git sees
  them as prunable, and Orca's prune after a worktree removal would delete their
  metadata.
- Optionally writes core.autocrlf=true into the repo when Windows Git uses it, so WSL
  Git in the project's terminals stops seeing CRLF changes. Never core.checkStat.

IPC is refused off Windows and with the internal WSL hosts gate closed.

Co-Authored-By: Claude <noreply@anthropic.com>

* test(wsl): mock the drive-project IPC in the core handler registration test

Co-Authored-By: Claude <noreply@anthropic.com>

* test(wsl): pin WSL Git for a drive project that never chose Windows Git, and record which Git ran

* fix(wsl): base-ref lookups and the remote-identity sweep run Git where the project's runtime does

* fix(wsl): route base-ref and remote-identity reads by runtime only with WSL hosts on

These reads ran host Git on main. Routing them into a WSL-runtime project's
distro is part of D1c, so with the WSL hosts gate closed they run host Git
exactly as before.

(cherry picked from commit 2ed82987dc5b82c3a86ea7e03ce05e9efc3e77fb)

* refactor(ipc): register the filesystem handlers in one call, keeping core registration under its line cap

The optional commit-message resolvers pass straight through, so the call is the same with or
without them. #34059 left this file at the 300-line cap and the WSL drive-project wiring adds to it.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(wsl): skip folder repos in the Windows Git check, and read worktree links with the prune guard's reader

Folder repos run no Git, so rev-parse on them always failed the preview. The link reader and
the WSL-authored test now come from worktree-prune-guard.ts; the copy's startsWith('/') wrongly
refused //wsl.localhost links, which Windows Git writes itself.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(wsl): the remote-identity sweep checks the distro a drive project's Git runs in, so it never boots a stopped one

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-28 15:15:26 -07:00

277 lines
9.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import type { Store } from './persistence'
import type { Project } from '../shared/project-types'
import type { Repo } from '../shared/repo-types'
import {
getLocalProjectGitExecOptions,
getWorktreeCreatePrefetchGitOptions,
getWorktreeMirrorDistro,
resolveLocalProjectRuntimeForRepo
} from './project-runtime-git-options'
import { _resetWslCachesForTests, _setWslCachesForTests } from './wsl'
function withPlatform<T>(platform: NodeJS.Platform, run: () => T): T {
const originalPlatform = process.platform
Object.defineProperty(process, 'platform', {
configurable: true,
value: platform
})
try {
return run()
} finally {
Object.defineProperty(process, 'platform', {
configurable: true,
value: originalPlatform
})
}
}
function makeRepo(overrides: Partial<Repo> = {}): Repo {
return {
id: 'repo-1',
displayName: 'Repo',
path: String.raw`C:\repo`,
badgeColor: '#000000',
addedAt: 0,
...overrides
}
}
function makeProject(overrides: Partial<Project> = {}): Project {
return {
id: 'project-1',
displayName: 'Project',
badgeColor: '#000000',
sourceRepoIds: ['repo-1'],
createdAt: 0,
updatedAt: 0,
...overrides
}
}
function makeStore(
project: Project,
localWindowsRuntimeDefault: unknown = { kind: 'windows-host' }
): Store {
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: runtime resolution reads only getProjects and getSettings.
return {
getProjects: () => [project],
getSettings: () => ({ localWindowsRuntimeDefault })
} as unknown as Store
}
describe('project runtime git options', () => {
afterEach(() => {
_resetWslCachesForTests()
})
it('does not probe or repair WSL git routing before capability caches exist', () => {
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
const options = withPlatform('win32', () =>
getLocalProjectGitExecOptions(makeStore(project), makeRepo())
)
expect(options).toEqual({ cwd: String.raw`C:\repo`, wslDistro: 'Ubuntu' })
})
it('keeps WSL Git for a drive project that inherits WSL and never chose Windows Git', () => {
const store = makeStore(makeProject(), { kind: 'wsl', distro: 'Ubuntu' })
const options = withPlatform('win32', () => getLocalProjectGitExecOptions(store, makeRepo()))
expect(options).toEqual({ cwd: String.raw`C:\repo`, wslDistro: 'Ubuntu' })
})
it('runs Windows Git for a WSL project converted to Windows Git', () => {
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu', git: 'windows-host' }
})
const options = withPlatform('win32', () =>
getLocalProjectGitExecOptions(makeStore(project), makeRepo())
)
expect(options).toEqual({ cwd: String.raw`C:\repo` })
})
it('returns repair state for missing cached WSL distro before local git execution', () => {
_setWslCachesForTests({ available: true, distros: ['Debian'] })
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
const runtime = withPlatform('win32', () =>
resolveLocalProjectRuntimeForRepo(makeStore(project), makeRepo())
)
expect(runtime).toEqual({
status: 'repair-required',
repair: {
projectId: 'project-1',
preferredRuntime: { kind: 'wsl', distro: 'Ubuntu' },
reason: 'wsl-distro-missing',
source: 'project-override',
cacheKey: 'project-1:repair:wsl-distro-missing:Ubuntu'
}
})
expect(() =>
withPlatform('win32', () => getLocalProjectGitExecOptions(makeStore(project), makeRepo()))
).toThrow('Project runtime requires repair before git execution: wsl-distro-missing')
})
it('returns repair state for cached WSL unavailable before local git execution', () => {
_setWslCachesForTests({ available: false, distros: [] })
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
expect(() =>
withPlatform('win32', () => getLocalProjectGitExecOptions(makeStore(project), makeRepo()))
).toThrow('Project runtime requires repair before git execution: wsl-unavailable')
})
it('keeps project host override on host even when cached WSL is unavailable', () => {
_setWslCachesForTests({ available: false, distros: [] })
const project = makeProject({
localWindowsRuntimePreference: { kind: 'windows-host' }
})
const options = withPlatform('win32', () =>
getLocalProjectGitExecOptions(makeStore(project), makeRepo())
)
expect(options).toEqual({ cwd: String.raw`C:\repo` })
})
it('does not apply local Windows runtime routing to SSH-owned repos', () => {
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
const runtime = withPlatform('win32', () =>
resolveLocalProjectRuntimeForRepo(
makeStore(project),
makeRepo({ connectionId: null, executionHostId: 'ssh:target-1' })
)
)
expect(runtime).toBeUndefined()
})
describe('getWorktreeMirrorDistro', () => {
it('names the distro a resolved WSL project runs in', () => {
_setWslCachesForTests({ available: true, distros: ['Ubuntu'] })
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
expect(
withPlatform('win32', () => getWorktreeMirrorDistro(makeStore(project), makeRepo()))
).toBe('Ubuntu')
})
it('names no distro for a project converted to Windows Git', () => {
_setWslCachesForTests({ available: true, distros: ['Ubuntu'] })
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu', git: 'windows-host' }
})
expect(
withPlatform('win32', () => getWorktreeMirrorDistro(makeStore(project), makeRepo()))
).toBeUndefined()
})
it('names no distro for a host-runtime project', () => {
const project = makeProject({ localWindowsRuntimePreference: { kind: 'windows-host' } })
expect(
withPlatform('win32', () => getWorktreeMirrorDistro(makeStore(project), makeRepo()))
).toBeUndefined()
})
// Worktree placement permits Windows fallback even when Git execution would require runtime repair.
it('names no distro instead of throwing when the runtime needs repair', () => {
_setWslCachesForTests({ available: true, distros: ['Debian'] })
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
expect(
withPlatform('win32', () => getWorktreeMirrorDistro(makeStore(project), makeRepo()))
).toBeUndefined()
})
it('names no distro when the store cannot resolve projects', () => {
_setWslCachesForTests({ available: true, distros: ['Ubuntu'] })
expect(withPlatform('win32', () => getWorktreeMirrorDistro({}, makeRepo()))).toBeUndefined()
})
})
describe('getWorktreeCreatePrefetchGitOptions', () => {
it('routes the warm-up through the distro a resolved WSL project runs in', () => {
_setWslCachesForTests({ available: true, distros: ['Ubuntu'] })
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
expect(
withPlatform('win32', () =>
getWorktreeCreatePrefetchGitOptions(makeStore(project), makeRepo())
)
).toEqual({ wslDistro: 'Ubuntu' })
})
// Speculative warmup must fall back to host Git instead of exposing a repair error.
it('falls back to host git instead of throwing when the runtime needs repair', () => {
_setWslCachesForTests({ available: true, distros: ['Debian'] })
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
expect(
withPlatform('win32', () =>
getWorktreeCreatePrefetchGitOptions(makeStore(project), makeRepo())
)
).toEqual({})
})
it('does not resolve a project runtime for folder workspaces', () => {
_setWslCachesForTests({ available: true, distros: ['Ubuntu'] })
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
const store = makeStore(project)
const getProjects = vi.fn(store.getProjects)
expect(
withPlatform('win32', () =>
getWorktreeCreatePrefetchGitOptions(
{ ...store, getProjects } as unknown as Store,
makeRepo({ kind: 'folder' })
)
)
).toEqual({})
expect(getProjects).not.toHaveBeenCalled()
})
})
it('does not apply local Windows runtime routing to runtime-owned repos', () => {
const project = makeProject({
localWindowsRuntimePreference: { kind: 'wsl', distro: 'Ubuntu' }
})
const runtime = withPlatform('win32', () =>
resolveLocalProjectRuntimeForRepo(
makeStore(project),
makeRepo({ connectionId: null, executionHostId: 'runtime:env-1' })
)
)
expect(runtime).toBeUndefined()
})
})