Add macOS iOS Android companion platform client wrappers
This commit is contained in:
@@ -2,6 +2,11 @@ export {
|
||||
CompanionRuntimeClient,
|
||||
GatewayRpcError,
|
||||
} from './runtimeClient.js';
|
||||
export {
|
||||
MacOSCompanionClient,
|
||||
IOSCompanionClient,
|
||||
AndroidCompanionClient,
|
||||
} from './platformClients.js';
|
||||
|
||||
export type {
|
||||
CompanionRuntimeClientOptions,
|
||||
@@ -23,3 +28,8 @@ export type {
|
||||
NodeStatus,
|
||||
NodePushSummary,
|
||||
} from './runtimeClient.js';
|
||||
export type {
|
||||
PlatformClientOptions,
|
||||
RegisterPushTokenInput,
|
||||
SharedStatusInput,
|
||||
} from './platformClients.js';
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
AndroidCompanionClient,
|
||||
IOSCompanionClient,
|
||||
MacOSCompanionClient,
|
||||
} from './platformClients.js';
|
||||
import type { CompanionRuntimeClient } from './runtimeClient.js';
|
||||
|
||||
function createRuntimeMock(): {
|
||||
runtime: CompanionRuntimeClient;
|
||||
connect: ReturnType<typeof vi.fn>;
|
||||
disconnect: ReturnType<typeof vi.fn>;
|
||||
registerNode: ReturnType<typeof vi.fn>;
|
||||
getNodeCapabilities: ReturnType<typeof vi.fn>;
|
||||
setNodeStatus: ReturnType<typeof vi.fn>;
|
||||
setNodeLocation: ReturnType<typeof vi.fn>;
|
||||
getNodeLocation: ReturnType<typeof vi.fn>;
|
||||
setNodePushToken: ReturnType<typeof vi.fn>;
|
||||
getSystemCapabilities: ReturnType<typeof vi.fn>;
|
||||
listSystemNodes: ReturnType<typeof vi.fn>;
|
||||
} {
|
||||
const connect = vi.fn(async () => undefined);
|
||||
const disconnect = vi.fn(() => undefined);
|
||||
const registerNode = vi.fn(async () => ({ registered: true }));
|
||||
const getNodeCapabilities = vi.fn(async () => ({ node: { id: 'n1', role: 'companion', registeredAt: Date.now() }, protocol: { serverVersion: 1, nodeVersion: 1, negotiatedVersion: 1 }, capabilities: { declared: [], enabled: [], featureGates: {} } }));
|
||||
const setNodeStatus = vi.fn(async () => ({ updated: true }));
|
||||
const setNodeLocation = vi.fn(async () => ({ updated: true }));
|
||||
const getNodeLocation = vi.fn(async () => ({ node: { id: 'n1', role: 'companion' }, location: null }));
|
||||
const setNodePushToken = vi.fn(async () => ({ updated: true }));
|
||||
const getSystemCapabilities = vi.fn(async () => ({ protocol: { version: 1 }, nodes: { enabled: true, locationEnabled: true, pushEnabled: true, allowedRoles: ['companion'], registered: true }, featureGates: {} }));
|
||||
const listSystemNodes = vi.fn(async () => ({ nodes: [], summary: { total: 0 } }));
|
||||
|
||||
const runtime = {
|
||||
connect,
|
||||
disconnect,
|
||||
registerNode,
|
||||
getNodeCapabilities,
|
||||
setNodeStatus,
|
||||
setNodeLocation,
|
||||
getNodeLocation,
|
||||
setNodePushToken,
|
||||
getSystemCapabilities,
|
||||
listSystemNodes,
|
||||
} as unknown as CompanionRuntimeClient;
|
||||
|
||||
return {
|
||||
runtime,
|
||||
connect,
|
||||
disconnect,
|
||||
registerNode,
|
||||
getNodeCapabilities,
|
||||
setNodeStatus,
|
||||
setNodeLocation,
|
||||
getNodeLocation,
|
||||
setNodePushToken,
|
||||
getSystemCapabilities,
|
||||
listSystemNodes,
|
||||
};
|
||||
}
|
||||
|
||||
describe('platform companion clients', () => {
|
||||
it('macOS client uses macos platform status and APNs push', async () => {
|
||||
const mock = createRuntimeMock();
|
||||
const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' });
|
||||
|
||||
await client.connect();
|
||||
await client.register();
|
||||
await client.setStatus({ appVersion: '1.0.0', powerSource: 'ac' });
|
||||
await client.setLocation({ latitude: 10, longitude: 20, source: 'manual' });
|
||||
await client.registerPushToken({ token: 'a'.repeat(64), topic: 'dev.flynn.macos', environment: 'production' });
|
||||
await client.listNodes();
|
||||
client.disconnect();
|
||||
|
||||
expect(mock.connect).toHaveBeenCalledOnce();
|
||||
expect(mock.registerNode).toHaveBeenCalledWith(expect.objectContaining({ nodeId: 'mac-node', role: 'companion' }));
|
||||
expect(mock.setNodeStatus).toHaveBeenCalledWith(expect.objectContaining({ platform: 'macos' }));
|
||||
expect(mock.setNodePushToken).toHaveBeenCalledWith(expect.objectContaining({ provider: 'apns', topic: 'dev.flynn.macos' }));
|
||||
expect(mock.listSystemNodes).toHaveBeenCalledWith({ platform: 'macos', role: 'companion' });
|
||||
expect(mock.disconnect).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('iOS client uses ios platform status and APNs push', async () => {
|
||||
const mock = createRuntimeMock();
|
||||
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
|
||||
|
||||
await client.register();
|
||||
await client.setStatus({ statusText: 'foreground', batteryPct: 52, powerSource: 'battery' });
|
||||
await client.registerPushToken({ token: 'b'.repeat(64), topic: 'dev.flynn.ios', environment: 'sandbox' });
|
||||
await client.listNodes();
|
||||
|
||||
expect(mock.setNodeStatus).toHaveBeenCalledWith(expect.objectContaining({ platform: 'ios' }));
|
||||
expect(mock.setNodePushToken).toHaveBeenCalledWith(expect.objectContaining({ provider: 'apns', environment: 'sandbox' }));
|
||||
expect(mock.listSystemNodes).toHaveBeenCalledWith({ platform: 'ios', role: 'companion' });
|
||||
});
|
||||
|
||||
it('android client uses android platform status and FCM push', async () => {
|
||||
const mock = createRuntimeMock();
|
||||
const client = new AndroidCompanionClient({ runtime: mock.runtime, nodeId: 'android-node' });
|
||||
|
||||
await client.register();
|
||||
await client.setStatus({ appVersion: '2.0.0', powerSource: 'battery' });
|
||||
await client.registerPushToken('c'.repeat(64));
|
||||
await client.listNodes();
|
||||
|
||||
expect(mock.setNodeStatus).toHaveBeenCalledWith(expect.objectContaining({ platform: 'android' }));
|
||||
expect(mock.setNodePushToken).toHaveBeenCalledWith({ provider: 'fcm', token: 'c'.repeat(64) });
|
||||
expect(mock.listSystemNodes).toHaveBeenCalledWith({ platform: 'android', role: 'companion' });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,248 @@
|
||||
import type {
|
||||
CompanionRuntimeClient,
|
||||
NodeCapabilitiesResult,
|
||||
NodeLocationGetResult,
|
||||
NodeLocationSetResult,
|
||||
NodeRegisterResult,
|
||||
NodeStatusSetResult,
|
||||
NodePushTokenSetResult,
|
||||
SetNodeLocationInput,
|
||||
SystemCapabilitiesResult,
|
||||
SystemNodesResult,
|
||||
} from './runtimeClient.js';
|
||||
|
||||
export interface PlatformClientOptions {
|
||||
runtime: CompanionRuntimeClient;
|
||||
nodeId: string;
|
||||
role?: string;
|
||||
capabilities?: string[];
|
||||
protocolVersion?: number;
|
||||
}
|
||||
|
||||
export interface RegisterPushTokenInput {
|
||||
token: string;
|
||||
topic?: string;
|
||||
environment?: 'sandbox' | 'production';
|
||||
}
|
||||
|
||||
export type SharedStatusInput = Omit<
|
||||
Parameters<CompanionRuntimeClient['setNodeStatus']>[0],
|
||||
'platform'
|
||||
>;
|
||||
|
||||
export class MacOSCompanionClient {
|
||||
private readonly runtime: CompanionRuntimeClient;
|
||||
private readonly nodeId: string;
|
||||
private readonly role: string;
|
||||
private readonly capabilities: string[];
|
||||
private readonly protocolVersion?: number;
|
||||
|
||||
constructor(options: PlatformClientOptions) {
|
||||
this.runtime = options.runtime;
|
||||
this.nodeId = options.nodeId;
|
||||
this.role = options.role ?? 'companion';
|
||||
this.capabilities = options.capabilities ?? ['ui.canvas', 'node.location.write', 'node.push.register'];
|
||||
this.protocolVersion = options.protocolVersion;
|
||||
}
|
||||
|
||||
connect(): Promise<void> {
|
||||
return this.runtime.connect();
|
||||
}
|
||||
|
||||
disconnect(): void {
|
||||
this.runtime.disconnect();
|
||||
}
|
||||
|
||||
register(): Promise<NodeRegisterResult> {
|
||||
return this.runtime.registerNode({
|
||||
nodeId: this.nodeId,
|
||||
role: this.role,
|
||||
protocolVersion: this.protocolVersion,
|
||||
capabilities: this.capabilities,
|
||||
});
|
||||
}
|
||||
|
||||
getCapabilities(): Promise<NodeCapabilitiesResult> {
|
||||
return this.runtime.getNodeCapabilities();
|
||||
}
|
||||
|
||||
setStatus(status: SharedStatusInput): Promise<NodeStatusSetResult> {
|
||||
return this.runtime.setNodeStatus({
|
||||
platform: 'macos',
|
||||
appVersion: status.appVersion,
|
||||
deviceName: status.deviceName,
|
||||
statusText: status.statusText,
|
||||
batteryPct: status.batteryPct,
|
||||
powerSource: status.powerSource,
|
||||
});
|
||||
}
|
||||
|
||||
setLocation(location: SetNodeLocationInput): Promise<NodeLocationSetResult> {
|
||||
return this.runtime.setNodeLocation(location);
|
||||
}
|
||||
|
||||
getLocation(): Promise<NodeLocationGetResult> {
|
||||
return this.runtime.getNodeLocation();
|
||||
}
|
||||
|
||||
registerPushToken(input: RegisterPushTokenInput): Promise<NodePushTokenSetResult> {
|
||||
return this.runtime.setNodePushToken({
|
||||
provider: 'apns',
|
||||
token: input.token,
|
||||
topic: input.topic,
|
||||
environment: input.environment,
|
||||
});
|
||||
}
|
||||
|
||||
getSystemCapabilities(): Promise<SystemCapabilitiesResult> {
|
||||
return this.runtime.getSystemCapabilities();
|
||||
}
|
||||
|
||||
listNodes(): Promise<SystemNodesResult> {
|
||||
return this.runtime.listSystemNodes({ platform: 'macos', role: this.role });
|
||||
}
|
||||
}
|
||||
|
||||
export class IOSCompanionClient {
|
||||
private readonly runtime: CompanionRuntimeClient;
|
||||
private readonly nodeId: string;
|
||||
private readonly role: string;
|
||||
private readonly capabilities: string[];
|
||||
private readonly protocolVersion?: number;
|
||||
|
||||
constructor(options: PlatformClientOptions) {
|
||||
this.runtime = options.runtime;
|
||||
this.nodeId = options.nodeId;
|
||||
this.role = options.role ?? 'companion';
|
||||
this.capabilities = options.capabilities ?? ['node.location.write', 'node.push.register'];
|
||||
this.protocolVersion = options.protocolVersion;
|
||||
}
|
||||
|
||||
connect(): Promise<void> {
|
||||
return this.runtime.connect();
|
||||
}
|
||||
|
||||
disconnect(): void {
|
||||
this.runtime.disconnect();
|
||||
}
|
||||
|
||||
register(): Promise<NodeRegisterResult> {
|
||||
return this.runtime.registerNode({
|
||||
nodeId: this.nodeId,
|
||||
role: this.role,
|
||||
protocolVersion: this.protocolVersion,
|
||||
capabilities: this.capabilities,
|
||||
});
|
||||
}
|
||||
|
||||
getCapabilities(): Promise<NodeCapabilitiesResult> {
|
||||
return this.runtime.getNodeCapabilities();
|
||||
}
|
||||
|
||||
setStatus(status: SharedStatusInput): Promise<NodeStatusSetResult> {
|
||||
return this.runtime.setNodeStatus({
|
||||
platform: 'ios',
|
||||
appVersion: status.appVersion,
|
||||
deviceName: status.deviceName,
|
||||
statusText: status.statusText,
|
||||
batteryPct: status.batteryPct,
|
||||
powerSource: status.powerSource,
|
||||
});
|
||||
}
|
||||
|
||||
setLocation(location: SetNodeLocationInput): Promise<NodeLocationSetResult> {
|
||||
return this.runtime.setNodeLocation(location);
|
||||
}
|
||||
|
||||
getLocation(): Promise<NodeLocationGetResult> {
|
||||
return this.runtime.getNodeLocation();
|
||||
}
|
||||
|
||||
registerPushToken(input: RegisterPushTokenInput): Promise<NodePushTokenSetResult> {
|
||||
return this.runtime.setNodePushToken({
|
||||
provider: 'apns',
|
||||
token: input.token,
|
||||
topic: input.topic,
|
||||
environment: input.environment,
|
||||
});
|
||||
}
|
||||
|
||||
getSystemCapabilities(): Promise<SystemCapabilitiesResult> {
|
||||
return this.runtime.getSystemCapabilities();
|
||||
}
|
||||
|
||||
listNodes(): Promise<SystemNodesResult> {
|
||||
return this.runtime.listSystemNodes({ platform: 'ios', role: this.role });
|
||||
}
|
||||
}
|
||||
|
||||
export class AndroidCompanionClient {
|
||||
private readonly runtime: CompanionRuntimeClient;
|
||||
private readonly nodeId: string;
|
||||
private readonly role: string;
|
||||
private readonly capabilities: string[];
|
||||
private readonly protocolVersion?: number;
|
||||
|
||||
constructor(options: PlatformClientOptions) {
|
||||
this.runtime = options.runtime;
|
||||
this.nodeId = options.nodeId;
|
||||
this.role = options.role ?? 'companion';
|
||||
this.capabilities = options.capabilities ?? ['node.location.write', 'node.push.register'];
|
||||
this.protocolVersion = options.protocolVersion;
|
||||
}
|
||||
|
||||
connect(): Promise<void> {
|
||||
return this.runtime.connect();
|
||||
}
|
||||
|
||||
disconnect(): void {
|
||||
this.runtime.disconnect();
|
||||
}
|
||||
|
||||
register(): Promise<NodeRegisterResult> {
|
||||
return this.runtime.registerNode({
|
||||
nodeId: this.nodeId,
|
||||
role: this.role,
|
||||
protocolVersion: this.protocolVersion,
|
||||
capabilities: this.capabilities,
|
||||
});
|
||||
}
|
||||
|
||||
getCapabilities(): Promise<NodeCapabilitiesResult> {
|
||||
return this.runtime.getNodeCapabilities();
|
||||
}
|
||||
|
||||
setStatus(status: SharedStatusInput): Promise<NodeStatusSetResult> {
|
||||
return this.runtime.setNodeStatus({
|
||||
platform: 'android',
|
||||
appVersion: status.appVersion,
|
||||
deviceName: status.deviceName,
|
||||
statusText: status.statusText,
|
||||
batteryPct: status.batteryPct,
|
||||
powerSource: status.powerSource,
|
||||
});
|
||||
}
|
||||
|
||||
setLocation(location: SetNodeLocationInput): Promise<NodeLocationSetResult> {
|
||||
return this.runtime.setNodeLocation(location);
|
||||
}
|
||||
|
||||
getLocation(): Promise<NodeLocationGetResult> {
|
||||
return this.runtime.getNodeLocation();
|
||||
}
|
||||
|
||||
registerPushToken(token: string): Promise<NodePushTokenSetResult> {
|
||||
return this.runtime.setNodePushToken({
|
||||
provider: 'fcm',
|
||||
token,
|
||||
});
|
||||
}
|
||||
|
||||
getSystemCapabilities(): Promise<SystemCapabilitiesResult> {
|
||||
return this.runtime.getSystemCapabilities();
|
||||
}
|
||||
|
||||
listNodes(): Promise<SystemNodesResult> {
|
||||
return this.runtime.listSystemNodes({ platform: 'android', role: this.role });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user