1import { mkdirSync } from 'fs';
2
3import { getAccountUsername } from '../getAccountUsername';
4import { getExpoHomeDirectory, getUserState } from '../getUserState';
5
6jest.mock('os');
7jest.mock('fs');
8
9describe(getAccountUsername, () => {
10  beforeEach(() => {
11    delete process.env.EXPO_CLI_USERNAME;
12    delete process.env.EAS_BUILD_USERNAME;
13  });
14
15  it(`gets the account name from EXPO_CLI_USERNAME`, () => {
16    process.env.EXPO_CLI_USERNAME = 'expo-cli-username';
17    process.env.EAS_BUILD_USERNAME = 'eas-build-username';
18    expect(getAccountUsername()).toBe('expo-cli-username');
19  });
20  it(`gets the account name from EAS_BUILD_USERNAME`, () => {
21    process.env.EAS_BUILD_USERNAME = 'eas-build-username';
22    expect(getAccountUsername()).toBe('eas-build-username');
23  });
24  it(`gets the account name from owner`, () => {
25    process.env.EXPO_CLI_USERNAME = 'expo-cli-username';
26    process.env.EAS_BUILD_USERNAME = 'eas-build-username';
27    expect(getAccountUsername({ owner: 'owner-username' })).toBe('owner-username');
28  });
29  it(`gets the account name from owner 2`, () => {
30    expect(getAccountUsername({ owner: 'owner-username' })).toBe('owner-username');
31  });
32  it(`uses anonymous name`, () => {
33    // Ensure the test doesn't interact with the developer's state.json
34    expect(getExpoHomeDirectory()).toBe('/home/.expo');
35    expect(getAccountUsername()).toBe('anonymous');
36  });
37  it(`uses previously authenticated username`, async () => {
38    // Ensure the test doesn't interact with the developer's state.json
39    expect(getExpoHomeDirectory()).toBe('/home/.expo');
40    // Ensure the dir exists
41    await mkdirSync(getExpoHomeDirectory(), { recursive: true });
42    // Set a username...
43    await getUserState().setAsync('auth', { username: 'bacon-boi' });
44    // Check the username...
45    expect(getAccountUsername()).toBe('bacon-boi');
46  });
47});
48