Files
orca/src/main/runtime/runtime-reference-catalog.test.ts
T
Neil 15717a4e53 Add CLI commands to manage and find workspace references (#26807)
* Add CLI commands to manage and find workspace references

* Fix reference review findings: source-context twins, legacy link collapse, closed chats, old-host errors

- Keep same-URL links with different source contexts separate; CLI add/remove no longer drops a twin
- Restore merging a bare legacy link into its single matching URL link (no duplicate PR rows, no resurrection on older-client removal)
- Carry absorbed data through collapse chains; resolve repoId conflicts deterministically
- Identity: ignore www., case-insensitive GitHub/GitLab/Gitea paths (stored URL keeps case), no Jira match on git hosts; find accepts self-hosted issue URLs
- reference find: skip closed native chats, require a session id for linked, include an explicitly named archived workspace, warm-cache fallback for --worktree
- Selector parity: collapse same-host duplicate paths, issue: ignores unlinked rows
- CLI: clear update-host error for hosts without reference.*, local flag checks before host probe, help text fixes

* Fix CI: add reference rm alias for verb policy; expect linkedItems in worktree ps summary

* Simplify reference CLI: share identifier/legacy-match helpers, drop redundant dedupe
2026-10-09 22:22:01 -07:00

136 lines
5.3 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
listReferenceWorkspaces,
listWorkspaceReferences,
selectReferenceWorkspace
} from './runtime-reference-catalog'
import { WORKTREE_META_PERSISTED_DEFAULTS } from '../../shared/worktree/meta-persisted-defaults'
import type { Repo } from '../../shared/repo-types'
import type { WorktreeMeta } from '../../shared/worktree/meta-types'
const repo: Repo = { id: 'repo', path: '/repo', displayName: 'api', badgeColor: '', addedAt: 0 }
const meta: WorktreeMeta = {
...WORKTREE_META_PERSISTED_DEFAULTS,
displayName: 'api',
linkedPR: 5123,
comment: '',
isUnread: false,
sortOrder: 0,
lastActivityAt: 0
}
const id = 'repo::/repo'
function catalog(metadata: Record<string, WorktreeMeta> = { [id]: meta }) {
return listReferenceWorkspaces({ getRepos: () => [repo], getAllWorktreeMeta: () => metadata })
}
describe('reference metadata catalog', () => {
it('exposes old scalar links without a scan', () => {
const rows = catalog()
expect(rows[0].linkedItems).toEqual([{ provider: 'github', type: 'pr', number: 5123 }])
expect(listWorkspaceReferences(rows[0]).references[0]).toMatchObject({
selected: true,
number: 5123
})
})
it('finds the closest local workspace only for explicit current', () => {
const rows = catalog({ [id]: meta, 'repo::/repo/child': { ...meta, displayName: 'child' } })
expect(selectReferenceWorkspace(rows, 'current', '/repo/child/src').name).toBe('child')
expect(() => selectReferenceWorkspace(rows, 'current')).toThrow('working directory')
expect(() => selectReferenceWorkspace(rows, 'current', '/repository')).toThrow(
'selector_not_found'
)
})
it('includes discovered workspaces before their first metadata write', () => {
const [worktree] = catalog()
const rows = listReferenceWorkspaces(
{ getRepos: () => [repo], getAllWorktreeMeta: () => ({}) },
[worktree]
)
expect(rows).toHaveLength(1)
expect(selectReferenceWorkspace(rows, `id:${id}`).id).toBe(id)
})
it('resolves explicit WSL paths only with the caller distro evidence', () => {
const [row] = catalog()
const ubuntu = { ...row, path: '//wsl$/Ubuntu/home/me/api' }
const debian = { ...row, id: 'repo::debian', path: '//wsl$/Debian/home/me/api' }
expect(
selectReferenceWorkspace([ubuntu, debian], 'path:/home/me/api', '//wsl$/Ubuntu/home/me').id
).toBe(row.id)
expect(() => selectReferenceWorkspace([ubuntu, debian], 'path:/home/me/api')).toThrow(
'selector_not_found'
)
expect(() =>
selectReferenceWorkspace([ubuntu], 'path:/home/me/api', '//wsl$/Debian/home/me')
).toThrow('selector_not_found')
})
it('retains same-id rows on separate hosts and refuses an ambiguous id', () => {
const rows = listReferenceWorkspaces({
getRepos: () => [repo, { ...repo, connectionId: 'server' }],
getAllWorktreeMeta: () => ({}),
getAllWorktreeMetaForHost: (hostId) => ({ [id]: { ...meta, hostId } })
})
expect(rows.map((row) => row.hostId)).toEqual(['local', 'ssh:server'])
expect(() => selectReferenceWorkspace(rows, `id:${id}`)).toThrow('selector_ambiguous')
expect(selectReferenceWorkspace(rows, 'current', '/repo').hostId).toBe('local')
})
it('collapses duplicate same-host path registrations but not cross-host ones', () => {
const [row] = catalog()
const duplicate = { ...row, id: 'repo-copy::/repo' }
expect(selectReferenceWorkspace([row, duplicate], `path:${row.path}`).id).toBe(row.id)
expect(selectReferenceWorkspace([row, duplicate], 'current', '/repo/src').id).toBe(row.id)
expect(() =>
selectReferenceWorkspace([row, { ...duplicate, hostId: 'ssh:build' }], `path:${row.path}`)
).toThrow('selector_ambiguous')
})
it('does not match issue:null against unlinked workspaces', () => {
const [row] = catalog()
expect(() => selectReferenceWorkspace([row], 'issue:null')).toThrow('selector_not_found')
expect(selectReferenceWorkspace([{ ...row, linkedIssue: 42 }], 'issue:42').id).toBe(row.id)
})
it('does not attribute unqualified legacy metadata to colliding hosts', () => {
const rows = listReferenceWorkspaces({
getRepos: () => [repo, { ...repo, connectionId: 'server' }],
getAllWorktreeMeta: () => ({ [id]: meta })
})
expect(rows).toEqual([])
})
it('uses folder storage and exposes selected legacy tasks', () => {
const rows = listReferenceWorkspaces({
getRepos: () => [],
getAllWorktreeMeta: () => ({}),
getFolderWorkspaces: () => [
{
id: 'notes',
projectGroupId: 'group',
name: 'notes',
folderPath: 'C:\\notes',
linkedTask: {
provider: 'linear',
type: 'issue',
number: 0,
title: 'Task',
url: 'https://linear.app/acme/issue/STA-1234',
linearIdentifier: 'STA-1234'
},
comment: '',
isArchived: false,
isUnread: false,
isPinned: false,
sortOrder: 0,
lastActivityAt: 0,
createdAt: 0,
updatedAt: 0
}
]
})
expect(selectReferenceWorkspace(rows, 'path:C:/notes')).toMatchObject({
id: 'folder:notes',
kind: 'folder'
})
expect(listWorkspaceReferences(rows[0]).references).toEqual([
expect.objectContaining({ selected: true, identifier: 'STA-1234' })
])
})
})