Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/e2e/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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<string, Promise<string>>();

Expand Down Expand Up @@ -308,6 +330,7 @@ export function createProvider(
...(options.maxDurationMin
? { maxDurationMin: options.maxDurationMin }
: {}),
...(locale ? { deviceLocale: locale } : {}),
},
request.signal,
);
Expand Down
30 changes: 30 additions & 0 deletions test/unit/e2e-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading