Repository navigation
fix(byo): tell two same-named computers apart by when each was added (TASK-650) #3029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
175a45c
fix(byo): tell two same-named computers apart by when each was added …
lilyshen0722 db6e926
fix(byo): date a shared computer name to the minute, with an id fallb…
lilyshen0722 9db709e
test(byo): pin the zh-CN shared-name label so an English date fails (…
lilyshen0722 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { machineDisplayName, defaultMachine } from '../utils/machineLabel'; | ||
|
|
||
| // The label is a function of the list it sits in: the same computer reads | ||
| // differently when a same-named sibling exists. t echoes the key and date so the | ||
| // assertions read the composed string. | ||
| const t = (key: string, options?: Record<string, string>) => `${key.split('.').pop()} ${options?.date ?? ''}`; | ||
|
|
||
| const older = { machineId: 'mach-old-1111', name: 'Sams-MacBook', createdAt: '2026-09-06T12:00:00.000Z' }; | ||
| const newer = { machineId: 'mach-new-2222', name: 'Sams-MacBook', createdAt: '2026-10-24T12:00:00.000Z' }; | ||
|
|
||
| test('a unique computer name is returned unchanged', () => { | ||
| expect(machineDisplayName(older, [older, { machineId: 'x', name: 'Studio' }], t, 'en')).toBe('Sams-MacBook'); | ||
| }); | ||
|
|
||
| test('a shared name gets the date it was added, and the two differ', () => { | ||
| const all = [newer, older]; | ||
| const a = machineDisplayName(newer, all, t, 'en'); | ||
| const b = machineDisplayName(older, all, t, 'en'); | ||
| expect(a).toMatch(/^Sams-MacBook · addedOn Oct 24, /); | ||
| expect(b).toMatch(/^Sams-MacBook · addedOn Sep 6, /); | ||
| expect(a).not.toBe(b); | ||
| }); | ||
|
|
||
| test('zh-CN reads the added date in Chinese, not English', () => { | ||
| const label = machineDisplayName(newer, [newer, older], (key, options) => `添加于 ${options?.date ?? key}`, 'zh-CN'); | ||
| // Chinese month and day, no English month. The 12:00Z fixture keeps the date | ||
| // the same from UTC-11 to UTC+11. | ||
| expect(label).toMatch(/^Sams-MacBook · 添加于 10月24日 /); | ||
| expect(label).not.toMatch(/Oct|AM|PM/); | ||
| }); | ||
|
|
||
| test('two computers added in the same minute still get distinct labels', () => { | ||
| const twinA = { machineId: 'mach-aaaa-0001', name: 'Sams-MacBook', createdAt: '2026-10-24T12:00:00.000Z' }; | ||
| const twinB = { machineId: 'mach-bbbb-0002', name: 'Sams-MacBook', createdAt: '2026-10-24T12:00:30.000Z' }; | ||
| const all = [twinA, twinB]; | ||
| expect(machineDisplayName(twinA, all, t, 'en')).toMatch(/ · 0001$/); | ||
| expect(machineDisplayName(twinB, all, t, 'en')).toMatch(/ · 0002$/); | ||
| }); | ||
|
|
||
| test('a shared name with no timestamp falls back to the machine ID tail', () => { | ||
| const undated = { machineId: 'mach-cccc-0003', name: 'Sams-MacBook', createdAt: null }; | ||
| expect(machineDisplayName(undated, [undated, newer], t, 'en')).toBe('Sams-MacBook · 0003'); | ||
| }); | ||
|
|
||
| test('the default is the first online computer, else the first listed', () => { | ||
| const online = { status: 'online' as const, id: 'a' }; | ||
| const offline = { status: 'offline' as const, id: 'b' }; | ||
| expect(defaultMachine([offline, online])).toBe(online); | ||
| expect(defaultMachine([offline])).toBe(offline); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Two computers can share a name (a reinstall registers a second one under the | ||
| // same hostname). A picker then needs something to tell them apart, so a name | ||
| // shared with another of the owner's computers gets when it was added. Two | ||
| // added in the same minute, or with no timestamp, fall back to the tail of the | ||
| // machine ID. A unique name is returned unchanged. | ||
| export interface NamedMachine { | ||
| machineId: string; | ||
| name: string; | ||
| createdAt?: string | null; | ||
| } | ||
|
|
||
| type Translate = (key: string, options?: Record<string, string>) => string; | ||
|
|
||
| export function machineDisplayName( | ||
| machine: NamedMachine, | ||
| all: NamedMachine[], | ||
| t: Translate, | ||
| language: string, | ||
| ): string { | ||
| const shared = all.filter((other) => other.machineId !== machine.machineId && other.name === machine.name); | ||
| if (shared.length === 0) return machine.name; | ||
| const idTail = machine.machineId.slice(-4); | ||
| if (!machine.createdAt) return `${machine.name} · ${idTail}`; | ||
| const addedAt = (m: NamedMachine) => new Date(m.createdAt as string).toLocaleString(language, { | ||
| month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit', | ||
| }); | ||
| const stamp = addedAt(machine); | ||
| const collides = shared.some((other) => other.createdAt && addedAt(other) === stamp); | ||
| return collides | ||
| ? `${machine.name} · ${t('agentByo.machine.addedOn', { date: stamp })} · ${idTail}` | ||
| : `${machine.name} · ${t('agentByo.machine.addedOn', { date: stamp })}`; | ||
| } | ||
|
|
||
| // The list arrives sorted by lastSeenAt, newest first. Prefer the first online | ||
| // computer, so the default is the one that can take the agent right now. | ||
| export function defaultMachine<T extends { status: 'online' | 'offline' }>(machines: T[]): T | undefined { | ||
| return machines.find((machine) => machine.status === 'online') ?? machines[0]; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.