forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevInstance.test.ts
More file actions
207 lines (190 loc) · 8.13 KB
/
Copy pathdevInstance.test.ts
File metadata and controls
207 lines (190 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { describe, expect, it } from 'vitest'
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import {
computeDevInstance,
deriveRendererPort,
deriveCdpPort,
findWorktreeContext,
sanitizeInstanceName,
DEV_RENDERER_BASE,
DEV_RENDERER_SPAN,
DEV_CDP_BASE,
DEV_CDP_SPAN,
PRIMARY_RENDERER_PORT,
PRIMARY_CDP_PORT
} from './devInstance'
describe('deriveRendererPort / deriveCdpPort', () => {
it('are deterministic for the same name', () => {
expect(deriveRendererPort('multi-worktree-dev')).toBe(deriveRendererPort('multi-worktree-dev'))
expect(deriveCdpPort('multi-worktree-dev')).toBe(deriveCdpPort('multi-worktree-dev'))
})
it('stay within their bands and never hit the primary ports', () => {
for (let i = 0; i < 200; i++) {
const name = `worktree-${i}`
const rp = deriveRendererPort(name)
const cp = deriveCdpPort(name)
expect(rp).toBeGreaterThanOrEqual(DEV_RENDERER_BASE)
expect(rp).toBeLessThan(DEV_RENDERER_BASE + DEV_RENDERER_SPAN)
expect(rp).not.toBe(PRIMARY_RENDERER_PORT)
expect(cp).toBeGreaterThanOrEqual(DEV_CDP_BASE)
expect(cp).toBeLessThan(DEV_CDP_BASE + DEV_CDP_SPAN)
expect(cp).not.toBe(PRIMARY_CDP_PORT)
}
})
it('disperses distinct worktree names across the renderer band', () => {
const ports = new Set<number>()
for (let i = 0; i < 40; i++) ports.add(deriveRendererPort(`feat-branch-${i}`))
// 40 names into 100 slots: a few collisions are expected (birthday paradox),
// but the spread must be strong — a broken hash would clump.
expect(ports.size).toBeGreaterThan(30)
})
it('renderer and cdp ports decorrelate for the same name (salting works)', () => {
// Without the 'cdp:' salt both bands would share the same low-bit offset,
// so the two offsets-within-band would always match. They must not.
let sameOffset = 0
for (let i = 0; i < 100; i++) {
const name = `wt-${i}`
if (deriveRendererPort(name) - DEV_RENDERER_BASE === deriveCdpPort(name) - DEV_CDP_BASE) {
sameOffset++
}
}
// Random chance of a matching offset is ~1/100 each; a handful is fine, but a
// correlated (unsalted) hash would match ~100/100.
expect(sameOffset).toBeLessThan(10)
})
})
describe('sanitizeInstanceName', () => {
it('lowercases and slugifies, stripping edge dashes', () => {
expect(sanitizeInstanceName('Feat/My Branch!')).toBe('feat-my-branch')
expect(sanitizeInstanceName(' UPPER_case.1 ')).toBe('upper_case.1')
})
it('never yields an empty string', () => {
expect(sanitizeInstanceName('///')).toBe('wt')
expect(sanitizeInstanceName('')).toBe('wt')
})
})
describe('computeDevInstance', () => {
it('primary checkout keeps the canonical ports and no title suffix', () => {
const inst = computeDevInstance('omi', true, {})
expect(inst).toEqual({
name: 'primary',
isPrimary: true,
rendererPort: PRIMARY_RENDERER_PORT,
cdpPort: PRIMARY_CDP_PORT,
titleSuffix: ''
})
})
it('linked worktree derives ports from the folder name and gets a title suffix', () => {
const inst = computeDevInstance('multi-worktree-dev', false, {})
expect(inst.isPrimary).toBe(false)
expect(inst.name).toBe('multi-worktree-dev')
expect(inst.rendererPort).toBe(deriveRendererPort('multi-worktree-dev'))
expect(inst.cdpPort).toBe(deriveCdpPort('multi-worktree-dev'))
expect(inst.titleSuffix).toBe(' — multi-worktree-dev')
})
it('OMI_INSTANCE=primary forces primary even from a linked worktree', () => {
const inst = computeDevInstance('some-worktree', false, { OMI_INSTANCE: 'primary' })
expect(inst.isPrimary).toBe(true)
expect(inst.rendererPort).toBe(PRIMARY_RENDERER_PORT)
})
it('OMI_INSTANCE=<name> forces a named instance even from the primary checkout', () => {
const inst = computeDevInstance('omi', true, { OMI_INSTANCE: 'my-exp' })
expect(inst.isPrimary).toBe(false)
expect(inst.name).toBe('my-exp')
expect(inst.rendererPort).toBe(deriveRendererPort('my-exp'))
})
it('OMI_DEV_PORT / OMI_DEV_CDP_PORT override the derived ports', () => {
const inst = computeDevInstance('wt', false, { OMI_DEV_PORT: '5210', OMI_DEV_CDP_PORT: '9250' })
expect(inst.rendererPort).toBe(5210)
expect(inst.cdpPort).toBe(9250)
})
it('ignores out-of-range / garbage port overrides and falls back to derived', () => {
const inst = computeDevInstance('wt', false, { OMI_DEV_PORT: '70000', OMI_DEV_CDP_PORT: 'abc' })
expect(inst.rendererPort).toBe(deriveRendererPort('wt'))
expect(inst.cdpPort).toBe(deriveCdpPort('wt'))
})
it('OMI_DEV_REMOTE_DEBUG wins over OMI_DEV_CDP_PORT for the CDP port', () => {
// OMI_DEV_REMOTE_DEBUG is the switch dev/bench.ts actually binds, so it must
// take precedence — otherwise seed-auth would target the wrong port.
const inst = computeDevInstance('wt', false, {
OMI_DEV_CDP_PORT: '9251',
OMI_DEV_REMOTE_DEBUG: '9333'
})
expect(inst.cdpPort).toBe(9333)
// Falls through to OMI_DEV_CDP_PORT when OMI_DEV_REMOTE_DEBUG is garbage.
const inst2 = computeDevInstance('wt', false, {
OMI_DEV_CDP_PORT: '9251',
OMI_DEV_REMOTE_DEBUG: 'nope'
})
expect(inst2.cdpPort).toBe(9251)
})
})
describe('findWorktreeContext', () => {
it('classifies a .git DIRECTORY as the primary checkout', () => {
const root = mkdtempSync(join(tmpdir(), 'omi-wt-primary-'))
try {
mkdirSync(join(root, '.git'))
const nested = join(root, 'desktop', 'windows')
mkdirSync(nested, { recursive: true })
const ctx = findWorktreeContext(nested)
expect(ctx.isPrimary).toBe(true)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('classifies a .git FILE (gitdir pointer) as a linked worktree and names it by folder', () => {
const parent = mkdtempSync(join(tmpdir(), 'omi-wt-linked-'))
try {
const wt = join(parent, 'my-feature')
const nested = join(wt, 'desktop', 'windows')
mkdirSync(nested, { recursive: true })
writeFileSync(join(wt, '.git'), 'gitdir: /somewhere/.git/worktrees/my-feature\n')
const ctx = findWorktreeContext(nested)
expect(ctx.isPrimary).toBe(false)
expect(ctx.name).toBe('my-feature')
} finally {
rmSync(parent, { recursive: true, force: true })
}
})
it('recovers a linked worktree whose .git pointer was deleted (Windows worktree bug)', () => {
// A linked worktree nested at <primary>/.worktrees/<name>. Simulate the known
// Windows bug where the worktree's `.git` pointer FILE vanishes: the walk then
// reaches the parent primary's `.git` DIRECTORY. It must NOT misdetect as
// primary (which would bind 5179 + the default profile and clobber the real
// session) — it recovers the linked identity from the `.worktrees/<name>` path.
const primary = mkdtempSync(join(tmpdir(), 'omi-wt-orphan-'))
try {
mkdirSync(join(primary, '.git')) // primary root: .git DIRECTORY
const nested = join(primary, '.worktrees', 'my-feature', 'desktop', 'windows')
mkdirSync(nested, { recursive: true })
// NOTE: intentionally no `.git` file at the worktree root (deleted).
const ctx = findWorktreeContext(nested)
expect(ctx.isPrimary).toBe(false)
expect(ctx.name).toBe('my-feature')
} finally {
rmSync(primary, { recursive: true, force: true })
}
})
it('a real primary checkout (no .worktrees on the path) still classifies as primary', () => {
const root = mkdtempSync(join(tmpdir(), 'omi-wt-realprimary-'))
try {
mkdirSync(join(root, '.git'))
const nested = join(root, 'desktop', 'windows')
mkdirSync(nested, { recursive: true })
expect(findWorktreeContext(nested).isPrimary).toBe(true)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('falls back to primary when no .git is found', () => {
const root = mkdtempSync(join(tmpdir(), 'omi-wt-none-'))
try {
const ctx = findWorktreeContext(root)
expect(ctx).toEqual({ name: 'primary', isPrimary: true })
} finally {
rmSync(root, { recursive: true, force: true })
}
})
})