Skip to content

Commit

Permalink
Don't attempt to search for USB phone devices (#206)
Browse files Browse the repository at this point in the history
Signed-off-by: Liam McLoughlin <lmcloughlin@google.com>
  • Loading branch information
Hexxeh committed Oct 6, 2022
1 parent d763b19 commit 8fc8b87
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
10 changes: 7 additions & 3 deletions packages/sdk-cli/src/commands/hosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ async function hostsAction(
cli: vorpal,
{ hostConnections }: { hostConnections: HostConnections },
) {
const hosts = await hostConnections.list();
const deviceHosts = await hostConnections.listOfType('device');
const phoneHosts = await hostConnections.listOfType('phone');

cli.activeCommand.log('Hosts:');
cli.activeCommand.log(hosts);
cli.activeCommand.log('Device Hosts:');
cli.activeCommand.log(deviceHosts);

cli.activeCommand.log('Phone Hosts:');
cli.activeCommand.log(phoneHosts);
}

export default function (stores: { hostConnections: HostConnections }) {
Expand Down
38 changes: 15 additions & 23 deletions packages/sdk-cli/src/models/HostConnections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,6 @@ describe('connect()', () => {
});
});

describe('list()', () => {
it('returns a list of both USB and Developer Relay hosts', () => {
developerRelaySpy.mockResolvedValue([relayHost]);
usbSpy.mockResolvedValue([usbHost]);
expect(hostConnections.list()).resolves.toEqual([relayHost, usbHost]);
});

it('throws if fetching developer relay hosts fails', () => {
developerRelaySpy.mockRejectedValue(new Error('fail devrelay'));
return expect(hostConnections.list()).rejects.toThrowError(
'An error was encountered when loading the list of available Developer Relay hosts: fail devrelay',
);
});

it('throws if fetching USB hosts fails', () => {
developerRelaySpy.mockResolvedValue([relayHost]);
usbSpy.mockRejectedValue(new Error('fail usb'));
return expect(hostConnections.list()).rejects.toThrowError(
'An error was encountered when loading the list of available USB hosts: fail usb',
);
});
});

describe('listOfType()', () => {
it('returns only app hosts', () => {
developerRelaySpy.mockResolvedValue([relayHost]);
Expand All @@ -147,4 +124,19 @@ describe('listOfType()', () => {
relayHost,
]);
});

it('throws if fetching USB hosts fails', () => {
developerRelaySpy.mockResolvedValue([relayHost]);
usbSpy.mockRejectedValue(new Error('fail usb'));
return expect(hostConnections.listOfType('device')).rejects.toThrowError(
'An error was encountered when loading the list of available USB hosts: fail usb',
);
});

it('throws if fetching developer relay hosts fails', () => {
developerRelaySpy.mockRejectedValue(new Error('fail devrelay'));
return expect(hostConnections.listOfType('phone')).rejects.toThrowError(
'An error was encountered when loading the list of available Developer Relay hosts: fail devrelay',
);
});
});
28 changes: 12 additions & 16 deletions packages/sdk-cli/src/models/HostConnections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class HostConnections {
return hostConnection;
}

async list() {
async listOfType(deviceType: DeviceType) {
const hosts: Host[] = [];

try {
Expand All @@ -113,24 +113,20 @@ class HostConnections {
);
}

try {
for (const device of await USBDebugHost.list()) {
hosts.push(device);
if (deviceType === 'device') {
try {
for (const device of await USBDebugHost.list()) {
hosts.push(device);
}
} catch (error) {
throw new Error(
`An error was encountered when loading the list of available USB hosts: ${
(error as Error).message
}`,
);
}
} catch (error) {
throw new Error(
`An error was encountered when loading the list of available USB hosts: ${
(error as Error).message
}`,
);
}

return hosts;
}

async listOfType(deviceType: DeviceType) {
const hosts = await this.list();

const hostRole = {
device: 'APP_HOST',
phone: 'COMPANION_HOST',
Expand Down

0 comments on commit 8fc8b87

Please sign in to comment.