Repository navigation
refactor: declare the profile fields each provider consumes, once #3168
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
10 commits
Select commit
Hold shift + click to select a range
3ab3c90
refactor(provider-webdriver): declare the profile fields each provide…
amankansal-lt ccd0e18
fix(provider-webdriver): refuse profile fields on a repeat allocation…
amankansal-lt 25a1ead
refactor(provider-webdriver): derive BrowserStack feature flags from …
amankansal-lt 7bd1bd2
fix(daemon): release a reused lease when its repeat allocation is can…
amankansal-lt 298e1a6
test: assert profile field refusals by code and flags, not message text
amankansal-lt 3c0fc29
fix(contracts): name a refused profile field's accepted aliases too
amankansal-lt ed606bf
refactor(daemon): settle a failed lease allocation in one helper
amankansal-lt e0425c8
test: move the connect profile-field refusals to their own file
amankansal-lt 2386729
test(limrun): refuse profile fields on an attached instance too
amankansal-lt 3d27e71
docs(daemon): keep the gone-requester doc comment on its function
amankansal-lt 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
27 changes: 27 additions & 0 deletions
27
packages/command-registry/src/__tests__/provider-profile-field-flags.test.ts
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,27 @@ | ||
| import { expect, test } from 'vitest'; | ||
| import type { ProviderProfileField } from '@agent-device/contracts/provider-profile-fields'; | ||
| import { | ||
| PROVIDER_PROFILE_FIELD_FLAG_ALIASES, | ||
| PROVIDER_PROFILE_FIELD_FLAGS, | ||
| } from '@agent-device/contracts/remote'; | ||
| import { getFlagDefinitionsForKey } from '../flag-registry.ts'; | ||
|
|
||
| // Refusals and BrowserStack feature errors name flags from this map, so each must be the CLI's | ||
| // canonical spelling of that field. | ||
| test('every provider profile field flag is the canonical CLI flag for its field', () => { | ||
| for (const [field, flag] of Object.entries(PROVIDER_PROFILE_FIELD_FLAGS) as Array< | ||
| [ProviderProfileField, string] | ||
| >) { | ||
| expect(getFlagDefinitionsForKey(field)[0]?.names[0], field).toBe(flag); | ||
| } | ||
| }); | ||
|
|
||
| // Contracts cannot depend on the command registry, so refusals carry their own copy of each field's | ||
| // accepted aliases; it must match the aliases the CLI parses. | ||
| test('every provider profile field lists the aliases the CLI accepts for it', () => { | ||
| for (const field of Object.keys(PROVIDER_PROFILE_FIELD_FLAGS) as ProviderProfileField[]) { | ||
| expect(PROVIDER_PROFILE_FIELD_FLAG_ALIASES[field] ?? [], field).toEqual( | ||
| getFlagDefinitionsForKey(field)[0]?.names.slice(1), | ||
| ); | ||
| } | ||
| }); |
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,77 @@ | ||
| import { test } from 'vitest'; | ||
| import assert from 'node:assert/strict'; | ||
| import { AppError } from '@agent-device/kernel/errors'; | ||
| import { | ||
| rejectRefusedProviderProfileFields, | ||
| type ProviderProfileFieldDeclaration, | ||
| } from './provider-profile-fields.ts'; | ||
|
|
||
| const DECLARATION: ProviderProfileFieldDeclaration = { | ||
| provider: 'fake', | ||
| label: 'Fake Cloud', | ||
| fields: { | ||
| providerApp: 'consumed', | ||
| providerOsVersion: 'refused', | ||
| providerProject: 'consumed', | ||
| providerBuild: 'consumed', | ||
| providerSessionName: 'consumed', | ||
| providerDeviceOrientation: 'consumed', | ||
| providerGeoLocation: 'refused', | ||
| providerTimezone: 'consumed', | ||
| providerAppiumVersion: 'consumed', | ||
| providerLanguage: 'consumed', | ||
| providerLocale: 'consumed', | ||
| providerNetworkProfile: 'consumed', | ||
| providerCustomNetwork: 'consumed', | ||
| providerNoResignApp: 'refused', | ||
| awsProjectArn: 'consumed', | ||
| awsDeviceArn: 'consumed', | ||
| awsAppArn: 'consumed', | ||
| awsRegion: 'consumed', | ||
| awsInteractionMode: 'consumed', | ||
| }, | ||
| }; | ||
|
|
||
| test('consumed, unset, empty, and false fields pass', () => { | ||
| assert.doesNotThrow(() => rejectRefusedProviderProfileFields(undefined, DECLARATION)); | ||
| assert.doesNotThrow(() => | ||
| rejectRefusedProviderProfileFields( | ||
| { | ||
| providerApp: 'app', | ||
| providerOsVersion: '', | ||
| providerGeoLocation: undefined, | ||
| providerNoResignApp: false, | ||
| unrelated: 'x', | ||
| }, | ||
| DECLARATION, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| test('a refused field fails with its flag, its aliases, and the provider named', () => { | ||
| assert.throws( | ||
| () => rejectRefusedProviderProfileFields({ providerOsVersion: '18' }, DECLARATION), | ||
| (error: unknown) => | ||
| error instanceof AppError && | ||
| error.code === 'INVALID_ARGS' && | ||
| error.message === '--provider-os-version (--os-version) is not supported by Fake Cloud.' && | ||
| error.details?.provider === 'fake' && | ||
| JSON.stringify(error.details?.flags) === '["--provider-os-version"]', | ||
| ); | ||
| }); | ||
|
|
||
| test('every refused field is reported at once', () => { | ||
| assert.throws( | ||
| () => | ||
| rejectRefusedProviderProfileFields( | ||
| { providerGeoLocation: 'US', providerNoResignApp: true, providerOsVersion: '18' }, | ||
| DECLARATION, | ||
| ), | ||
| (error: unknown) => | ||
| error instanceof AppError && | ||
| error.message === | ||
| '--provider-os-version (--os-version), --provider-geo-location (--geo-location), --provider-no-resign-app are not supported by Fake Cloud.' && | ||
| JSON.stringify(error.details?.flags) === | ||
| '["--provider-os-version","--provider-geo-location","--provider-no-resign-app"]', | ||
| ); | ||
| }); |
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,54 @@ | ||
| import { AppError } from '@agent-device/kernel/errors'; | ||
| import { | ||
| PROVIDER_PROFILE_FIELD_FLAG_ALIASES, | ||
| PROVIDER_PROFILE_FIELD_FLAGS, | ||
| type CloudProviderProfileFields, | ||
| } from './remote-config-fields.ts'; | ||
|
|
||
| export type ProviderProfileField = keyof CloudProviderProfileFields; | ||
|
|
||
| /** | ||
| * What one lease provider does with every Cloud provider profile field. The record is total, so a | ||
| * field added to the profile fails to build until each provider says whether it consumes it; a | ||
| * field a provider neither reads nor refuses would otherwise ride the profile and be dropped. | ||
| */ | ||
| export type ProviderProfileFieldDeclaration = Readonly<{ | ||
| provider: string; | ||
| label: string; | ||
| fields: Readonly<Record<ProviderProfileField, 'consumed' | 'refused'>>; | ||
| }>; | ||
|
|
||
| /** | ||
| * Fails when `flags` set a profile field the provider refuses. Every route to a provider — connect, | ||
| * `leases.allocate`, a hand-authored remote-config profile — runs this against the same declaration. | ||
| */ | ||
| export function rejectRefusedProviderProfileFields( | ||
| flags: Readonly<Record<string, unknown>> | undefined, | ||
| declaration: ProviderProfileFieldDeclaration, | ||
| ): void { | ||
| const refused = (Object.keys(declaration.fields) as ProviderProfileField[]).filter( | ||
| (field) => declaration.fields[field] === 'refused' && isSet(flags?.[field]), | ||
| ); | ||
| if (refused.length === 0) return; | ||
| const plural = refused.length !== 1; | ||
| throw new AppError( | ||
| 'INVALID_ARGS', | ||
| `${refused.map(describeFlag).join(', ')} ${plural ? 'are' : 'is'} not supported by ${declaration.label}.`, | ||
| { | ||
| hint: `Drop ${plural ? 'those flags' : 'the flag'} or use a provider that supports ${plural ? 'them' : 'it'}.`, | ||
| provider: declaration.provider, | ||
| flags: refused.map((field) => PROVIDER_PROFILE_FIELD_FLAGS[field]), | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // The flags bag keeps the field, not the spelling, so name every spelling the user could have typed. | ||
| function describeFlag(field: ProviderProfileField): string { | ||
| const aliases = PROVIDER_PROFILE_FIELD_FLAG_ALIASES[field]; | ||
| const flag = PROVIDER_PROFILE_FIELD_FLAGS[field]; | ||
| return aliases?.length ? `${flag} (${aliases.join(', ')})` : flag; | ||
| } | ||
|
|
||
| function isSet(value: unknown): boolean { | ||
| return value !== undefined && value !== null && value !== false && value !== ''; | ||
| } |
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
Oops, something went wrong.
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.