Skip to content

Commit

Permalink
VBLOCKS-1578 | Adding typechecks to all public APIs (twilio#168)
Browse files Browse the repository at this point in the history
* VBLOCKS-1578 | Adding typechecks to all public APIs

* Remove unnecessary file
  • Loading branch information
charliesantos authored Jun 7, 2023
1 parent e375c4b commit 1aba954
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/twilio/rtc/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

/**
* Sample RTC statistics. See [[Connection.sampleEvent]]
* Sample RTC statistics. See [[Call.sampleEvent]]
*/
export default interface RTCSample {
[key: string]: any;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"test:integration": "karma start $PWD/karma.conf.ts --log-level debug",
"test:network": "node ./scripts/karma.js $PWD/karma.network.conf.ts",
"test:selenium": "mocha tests/browser/index.js",
"test:typecheck": "./node_modules/typescript/bin/tsc tests/typecheck/index.ts --noEmit",
"test:typecheck": "./node_modules/typescript/bin/tsc tests/typecheck/*.ts --noEmit",
"test:unit": "nyc mocha -r ts-node/register ./tests/index.ts",
"test:webpack": "cd ./tests/webpack && npm install && npm test"
},
Expand Down
40 changes: 40 additions & 0 deletions tests/typecheck/audiohelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Device } from '../../';

const checkAudioHelper = async () => {
const device: Device = new Device('foo', {});
const audio = device.audio;

if (!audio) {
return;
}

const availableInputDevices: Map<string, MediaDeviceInfo> = audio.availableInputDevices;
const availableOutputDevices: Map<string, MediaDeviceInfo> = audio.availableOutputDevices;
const isOutputSelectionSupported: boolean = audio.isOutputSelectionSupported;
const isVolumeSupported: boolean = audio.isVolumeSupported;
const audioConstraints: MediaTrackConstraints | null = audio.audioConstraints;
const inputDevice: MediaDeviceInfo | null = audio.inputDevice;
const inputStream: MediaStream | null = audio.inputStream;

[audio.ringtoneDevices, audio.speakerDevices].forEach(deviceCollection => {
let d: Set<MediaDeviceInfo> = deviceCollection.get();
deviceCollection.delete(d.values()[0]);
deviceCollection.set('');
deviceCollection.set(['foo', 'bar']);
deviceCollection.test('');
deviceCollection.test();
});

[undefined, true, false].forEach((doEnable) => {
audio.disconnect(doEnable);
audio.incoming(doEnable);
audio.outgoing(doEnable);
});

await audio.setAudioConstraints({});
await audio.setInputDevice('foo');
await audio.unsetAudioConstraints();
await audio.unsetInputDevice();
};

export default checkAudioHelper;
40 changes: 40 additions & 0 deletions tests/typecheck/call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Call, Device } from '../../';

const checkCall = async () => {
const call: Call = await (new Device('foo', {})).connect();

call.on('messageReceived', (message: Call.Message) => {
const content: string = message.content;
const contentType: string | undefined = message.contentType;
const messageType: Call.MessageType = message.messageType;
const voiceEventSid: string | undefined = message.voiceEventSid;
});

const isVerified: boolean | undefined = call.callerInfo?.isVerified;
const customParameters: Map<string, string> = call.customParameters;
const outboundConnectionId: string | undefined = call.outboundConnectionId;
const parameters: Record<string, string> = call.parameters;

const codec: string = call.codec;
const direction: Call.CallDirection = call.direction;

call.accept({
rtcConfiguration: {},
rtcConstraints: {},
});

call.disconnect();
call.ignore();
call.mute();
call.reject();
call.sendDigits('foo');
call.sendMessage({ content: 'foo', messageType: Call.MessageType.UserDefinedMessage });

await call.postFeedback(Call.FeedbackScore.One, Call.FeedbackIssue.AudioLatency);
const isMuted: boolean = call.isMuted();
const localStream: MediaStream | undefined = call.getLocalStream();
const remoteStream: MediaStream | undefined = call.getRemoteStream();
const status: Call.State = call.status();
};

export default checkCall;
76 changes: 76 additions & 0 deletions tests/typecheck/device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Call, Device, PreflightTest } from '../../';

const checkDevice = async () => {
const options: Device.Options = {
RTCPeerConnection: 'foo',
allowIncomingWhileBusy: true,
appName: 'foo',
appVersion: 'foo',
closeProtection: true,
codecPreferences: [Call.Codec.Opus, Call.Codec.PCMU],
disableAudioContextSounds: true,
dscp: true,
edge: 'foo',
enumerateDevices: 'foo',
forceAggressiveIceNomination: true,
getUserMedia: 'foo',
logLevel: 'debug',
maxAverageBitrate: 1,
maxCallSignalingTimeoutMs: 1,
sounds: {
[Device.SoundName.Disconnect]: 'foo',
[Device.SoundName.Dtmf0]: 'foo',
[Device.SoundName.Dtmf1]: 'foo',
[Device.SoundName.Dtmf2]: 'foo',
[Device.SoundName.Dtmf3]: 'foo',
[Device.SoundName.Dtmf4]: 'foo',
[Device.SoundName.Dtmf5]: 'foo',
[Device.SoundName.Dtmf6]: 'foo',
[Device.SoundName.Dtmf7]: 'foo',
[Device.SoundName.Dtmf8]: 'foo',
[Device.SoundName.Dtmf9]: 'foo',
[Device.SoundName.DtmfH]: 'foo',
[Device.SoundName.DtmfS]: 'foo',
[Device.SoundName.Incoming]: 'foo',
[Device.SoundName.Outgoing]: 'foo',
},
tokenRefreshMs: 1,
};
const device: Device = new Device('foo', options);

const isSupported: boolean = Device.isSupported;
const packageName: string = Device.packageName;
const version: string = Device.version;

const calls: Call[] = device.calls;
const isBusy: boolean = device.isBusy;
const state: Device.State = device.state;

const edge: string | null = device.edge;
const home: string | null = device.home;
const identity: string | null = device.identity;
const token: string | null = device.token;

await device.connect({
params: { To: 'foo' },
rtcConfiguration: {},
rtcConstraints: {},
});
device.destroy();
device.disconnectAll();
await device.register();
await device.unregister();
device.updateOptions(options);
device.updateToken('foo');

const preflight: PreflightTest = Device.runPreflight('foo', {
codecPreferences: [Call.Codec.Opus, Call.Codec.PCMU],
edge: 'foo',
fakeMicInput: true,
iceServers: [],
logLevel: 'debug',
signalingTimeoutMs: 1,
});
};

export default checkDevice;
15 changes: 0 additions & 15 deletions tests/typecheck/index.ts

This file was deleted.

96 changes: 96 additions & 0 deletions tests/typecheck/preflight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Call, Device, PreflightTest } from '../../';

const checkPreflight = async () => {
const preflight: PreflightTest = Device.runPreflight('foo', {
codecPreferences: [Call.Codec.Opus, Call.Codec.PCMU],
edge: 'foo',
fakeMicInput: true,
iceServers: [],
logLevel: 'debug',
signalingTimeoutMs: 1,
});

const callSid: string | undefined = preflight.callSid;
const endTime: number | undefined = preflight.endTime;
const sample = preflight.latestSample;
if (sample) {
const audioInputLevel: number = sample.audioInputLevel;
const audioOutputLevel: number = sample.audioOutputLevel;
const bytesReceived: number = sample.bytesReceived;
const bytesSent: number = sample.bytesSent;
const jitter: number = sample.jitter;
const mos: number | null = sample.mos;
const packetsLost: number = sample.packetsLost;
const packetsLostFraction: number = sample.packetsLostFraction;
const packetsReceived: number = sample.packetsReceived;
const packetsSent: number = sample.packetsSent;
const rtt: number = sample.rtt;
const timestamp: number = sample.timestamp;
const totalBytesReceived: number = sample.totals.bytesReceived;
const totalBytesSent: number = sample.totals.bytesSent;
const totalPacketsLost: number = sample.totals.packetsLost;
const totalPacketsLostFraction: number = sample.totals.packetsLostFraction;
const totalPacketsReceived: number = sample.totals.packetsReceived;
const totalPacketsSent: number = sample.totals.packetsSent;
}
const report: PreflightTest.Report | undefined = preflight.report;
if (report) {
const callQuality: PreflightTest.CallQuality | undefined = report.callQuality;
const callSid: string | undefined = report.callSid;
const edge: string | undefined = report.edge;
const iceCandidateStats: PreflightTest.RTCIceCandidateStats[] = report.iceCandidateStats;
const isTurnRequired: undefined | false | true = report.isTurnRequired;

const dtlsStart: number | undefined = report.networkTiming.dtls?.start;
const dtlsEnd: number | undefined = report.networkTiming.dtls?.end;
const dtlsDuration: number | undefined = report.networkTiming.dtls?.duration;
const iceStart: number | undefined = report.networkTiming.ice?.start;
const iceEnd: number | undefined = report.networkTiming.ice?.end;
const iceDuration: number | undefined = report.networkTiming.ice?.duration;
const peerConnectionStart: number | undefined = report.networkTiming.peerConnection?.start;
const peerConnectionEnd: number | undefined = report.networkTiming.peerConnection?.end;
const peerConnectionDuration: number | undefined = report.networkTiming.peerConnection?.duration;
const signalingStart: number | undefined = report.networkTiming.signaling?.start;
const signalingEnd: number | undefined = report.networkTiming.signaling?.end;
const signalingDuration: number | undefined = report.networkTiming.signaling?.duration;

const samples: typeof preflight.latestSample[] = report.samples;
const selectedEdge: string | undefined = report.selectedEdge;
const selectedIceCandidatePairStats: PreflightTest.RTCSelectedIceCandidatePairStats | undefined = report.selectedIceCandidatePairStats;
const localCandidate: PreflightTest.RTCIceCandidateStats = selectedIceCandidatePairStats?.localCandidate;
const remoteCandidate: PreflightTest.RTCIceCandidateStats = selectedIceCandidatePairStats?.remoteCandidate;
const stats: PreflightTest.RTCStats | undefined = report.stats;
const jitterAverage: number | undefined = stats?.jitter.average;
const jitterMax: number | undefined = stats?.jitter.max;
const jitterMin: number | undefined = stats?.jitter.min;
const mosAverage: number | undefined = stats?.mos.average;
const mosMax: number | undefined = stats?.mos.max;
const mosMin: number | undefined = stats?.mos.min;
const rttAverage: number | undefined = stats?.rtt.average;
const rttMax: number | undefined = stats?.rtt.max;
const rttMin: number | undefined = stats?.rtt.min;
const testStart: number | undefined = report.testTiming.start;
const testEnd: number | undefined = report.testTiming.end;
const testDuration: number | undefined = report.testTiming.duration;

const totals = report.totals;
if (totals) {
const totalBytesReceived: number = totals.bytesReceived;
const totalBytesSent: number = totals.bytesSent;
const totalPacketsLost: number = totals.packetsLost;
const totalPacketsLostFraction: number = totals.packetsLostFraction;
const totalPacketsReceived: number = totals.packetsReceived;
const totalPacketsSent: number = totals.packetsSent;
}

const warnings: PreflightTest.Warning[] = report.warnings;
const name: string = warnings[0].name;
const description: string = warnings[0].description;
}

const startTime: number = preflight.startTime;
const status: PreflightTest.Status = preflight.status;
preflight.stop();
};

export default checkPreflight;
16 changes: 16 additions & 0 deletions tests/typecheck/twilioerror.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TwilioError } from '../../';

const checkTwilioError = async () => {
const error = new TwilioError.TwilioError;
const causes: string[] = error.causes;
const code: number = error.code;
const description: string = error.description;
const explanation: string = error.explanation;
const message: string = error.message;
const name: string = error.name;
const originalError: any = error.originalError;
const solutions: string[] = error.solutions;

};

export default checkTwilioError;

0 comments on commit 1aba954

Please sign in to comment.