diff --git a/README.md b/README.md index 8033e6d..3b402a4 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ export default { } satisfies E2EConfig; ``` -Run `npx e2e run` with `DEVICE_CLOUD_API_KEY` set. The provider uploads `appPath` once per run and each session installs it, so e2e installs nothing itself. To use a build you already uploaded, pass `deviceCloud({ appBinaryId })`. It also takes `model`, `os` and `maxDurationMin`. Each session's console link is printed as the run starts, and the session is stopped when e2e is done with it. +Run `npx e2e run` with `DEVICE_CLOUD_API_KEY` set. The provider uploads `appPath` once per run and each session installs it, so e2e installs nothing itself. To use a build you already uploaded, pass `deviceCloud({ appBinaryId })`. It also takes `model`, `os`, `maxDurationMin`, and `locale` (such as `en_US`) for the device's language and region, set before your build is installed. Each session's console link is printed as the run starts, and the session is stopped when e2e is done with it. ## Development diff --git a/src/e2e/provider.ts b/src/e2e/provider.ts index 29eeb45..a4295ea 100644 --- a/src/e2e/provider.ts +++ b/src/e2e/provider.ts @@ -82,6 +82,11 @@ export interface DeviceCloudOptions { os?: string; /** How long each session may last, in minutes. The organization's limit without. */ maxDurationMin?: number; + /** + * The device's locale, as `en_US` or `en-US`: set before the build is + * installed or opened. The device's own default without. + */ + locale?: string; } /** What the provider calls out to: the network, the upload, and the clock. */ @@ -189,10 +194,27 @@ function connection( return { apiKey, apiUrl: check.origin }; } +/** A locale as the API takes it, `en_US`, from that or `en-US`. */ +const LOCALE = /^[a-z]{2,3}_[A-Z]{2}$/; + +function deviceLocale(locale: string | undefined): string | undefined { + if (locale === undefined) return undefined; + const normalised = locale.trim().replace('-', '_'); + if (!LOCALE.test(normalised)) { + throw new Error( + `DeviceCloud: "${locale}" is not a locale. Use a language and region, such as en_US or en-US.`, + ); + } + + return normalised; +} + export function createProvider( options: DeviceCloudOptions, deps: ProviderDeps, ): DeviceCloudProvider { + // Checked when the config is read, before any session is started or billed. + const locale = deviceLocale(options.locale); // One upload per build per run: e2e acquires every worker slot at once. const uploads = new Map>(); @@ -308,6 +330,7 @@ export function createProvider( ...(options.maxDurationMin ? { maxDurationMin: options.maxDurationMin } : {}), + ...(locale ? { deviceLocale: locale } : {}), }, request.signal, ); diff --git a/test/unit/e2e-provider.test.ts b/test/unit/e2e-provider.test.ts index 5a7c95e..a43343f 100644 --- a/test/unit/e2e-provider.test.ts +++ b/test/unit/e2e-provider.test.ts @@ -197,6 +197,36 @@ describe('the e2e device provider', () => { ); }); + it('starts the device in the locale asked for, written either way', async () => { + for (const locale of ['en_US', 'en-US', ' en-US ']) { + const { api, provider } = setup(happyRoutes(), { locale }); + await provider.acquire(request()); + expect( + (api.calls[0].body as { deviceLocale?: string }).deviceLocale, + ).to.equal('en_US'); + } + + const { api, provider } = setup(happyRoutes(), { locale: 'pt-BR' }); + await provider.acquire(request()); + expect( + (api.calls[0].body as { deviceLocale?: string }).deviceLocale, + ).to.equal('pt_BR'); + }); + + it("leaves the device's own locale without one", async () => { + const { api, provider } = setup(happyRoutes()); + await provider.acquire(request()); + expect(api.calls[0].body).to.not.have.property('deviceLocale'); + }); + + it('refuses a locale it cannot send when the config is read, before any session starts', () => { + for (const locale of ['english', 'en_us', 'EN-US', 'en', 'en_USA', '']) { + expect(() => setup(happyRoutes(), { locale }), locale).to.throw( + 'is not a locale. Use a language and region, such as en_US or en-US.', + ); + } + }); + it('takes the key from the options before the environment', async () => { const { api, provider } = setup(happyRoutes(), { apiKey: 'key-from-options',