Skip to content

Commit

Permalink
Introduce status code to diagnostic routines
Browse files Browse the repository at this point in the history
  • Loading branch information
cyhuang1230 committed Jan 15, 2024
1 parent 5164611 commit 77d25a2
Show file tree
Hide file tree
Showing 5 changed files with 910 additions and 54 deletions.
196 changes: 178 additions & 18 deletions src/__tests__/dpsl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

const {dpsl} = require('../dpsl.js');
const {CATEGORY: STATUS_CODE_CATEGORY} = require('../status_codes.js');

describe('dpsl.telemetry tests', () => {
let originalChrome;
Expand Down Expand Up @@ -166,7 +167,8 @@ describe('dpsl.telemetry tests', () => {
'ipv6Addresses': [],
'signalStrength': 100,
},
]};
],
};

const chrome = {
os: {
Expand Down Expand Up @@ -240,7 +242,7 @@ describe('dpsl.telemetry tests', () => {
os: {
telemetry: {
getNonRemovableBlockDevicesInfo:
() => expectedNonRemovableBlockDevicesInfo,
() => expectedNonRemovableBlockDevicesInfo,
},
},
};
Expand Down Expand Up @@ -522,7 +524,10 @@ describe('dpsl.diagnostics tests', () => {
test('Routine.{getStatus(), resume(), stop()} returns correct data',
(done) => {
// Mock the global chrome object.
const expectedRunRoutineResponse = {id: 123456};
const expectedRunRoutineResponse = {
id: 123456,
category: STATUS_CODE_CATEGORY.BATTERY_AND_POWER,
};
const expectedRoutineStatusResponse = {
progress_percent: 76,
status: 'running',
Expand All @@ -532,9 +537,9 @@ describe('dpsl.diagnostics tests', () => {
os: {
diagnostics: {
runBatteryCapacityRoutine:
() => Promise.resolve(expectedRunRoutineResponse),
() => Promise.resolve(expectedRunRoutineResponse),
getRoutineUpdate:
() => Promise.resolve(expectedRoutineStatusResponse),
() => Promise.resolve(expectedRoutineStatusResponse),
},
},
};
Expand All @@ -557,147 +562,298 @@ describe('dpsl.diagnostics tests', () => {
});
});

test('Routine.getStatus() returns correct data when routine returns passed',
(done) => {
// Mock the global chrome object.
const expectedRunRoutineResponse = {
id: 123456,
category: STATUS_CODE_CATEGORY.STORAGE,
};
const expectedRoutineStatusResponseFromChrome = {
progress_percent: 100,
status: 'Passed',
status_message: 'smartctl-check status: PASS.',
};

const chrome = {
os: {
diagnostics: {
runSmartctlCheckRoutine:
() => Promise.resolve(expectedRunRoutineResponse),
getRoutineUpdate:
() => Promise.resolve(expectedRoutineStatusResponseFromChrome),
},
},
};
global.chrome = chrome;

dpsl.diagnostics.nvme.runSmartctlCheckRoutine().then((routine) => {
expect(routine).toEqual(expectedRunRoutineResponse);
routine.getStatus().then((value) => {
expect(value).toStrictEqual(
Object.assign(
{},
expectedRoutineStatusResponseFromChrome,
{status_code: 0x0010001},
));
done();
});
});
});

test('Routine.getStatus() returns correct data when routine returns error',
(done) => {
// Mock the global chrome object.
const expectedRunRoutineResponse = {
id: 123456,
category: STATUS_CODE_CATEGORY.STORAGE,
};
const expectedRoutineStatusResponseFromChrome = {
progress_percent: 100,
status: 'Error',
status_message: 'Failed to parse mmc output.',
};

const chrome = {
os: {
diagnostics: {
runEmmcLifetimeRoutine:
() => Promise.resolve(expectedRunRoutineResponse),
getRoutineUpdate:
() => Promise.resolve(expectedRoutineStatusResponseFromChrome),
},
},
};
global.chrome = chrome;

dpsl.diagnostics.emmc.runEmmcLifetimeRoutine().then((routine) => {
expect(routine).toEqual(expectedRunRoutineResponse);
routine.getStatus().then((value) => {
expect(value).toStrictEqual(
Object.assign(
{},
expectedRoutineStatusResponseFromChrome,
{status_code: 0x00E000D},
));
done();
});
});
});

test('Routine.getStatus() returns correct data when routine returns error ' +
'with dynamic status message',
(done) => {
// Mock the global chrome object.
const expectedRunRoutineResponse = {
id: 123456,
category: STATUS_CODE_CATEGORY.FAN,
};
const expectedRoutineStatusResponseFromChrome = {
progress_percent: 100,
status: 'Error',
status_message: 'Failed to read temperature for thermal sensor idx: 13',
};

const chrome = {
os: {
diagnostics: {
runFanRoutine:
() => Promise.resolve(expectedRunRoutineResponse),
getRoutineUpdate:
() => Promise.resolve(expectedRoutineStatusResponseFromChrome),
},
},
};
global.chrome = chrome;

dpsl.diagnostics.fan.runFanRoutine().then((routine) => {
expect(routine).toEqual(expectedRunRoutineResponse);
routine.getStatus().then((value) => {
expect(value).toStrictEqual(
Object.assign(
{},
expectedRoutineStatusResponseFromChrome,
{status_code: 0x0060008},
));
done();
});
});
});

const testCases = [
{
'dpslRoutineFunction': dpsl.diagnostics.power.runAcPowerRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.BATTERY_AND_POWER,
'chromeOsRoutineFunction': 'runAcPowerRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.battery.runCapacityRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.BATTERY_AND_POWER,
'chromeOsRoutineFunction': 'runBatteryCapacityRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.battery.runHealthRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.BATTERY_AND_POWER,
'chromeOsRoutineFunction': 'runBatteryHealthRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.battery.runDischargeRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.BATTERY_AND_POWER,
'chromeOsRoutineFunction': 'runBatteryDischargeRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.battery.runChargeRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.BATTERY_AND_POWER,
'chromeOsRoutineFunction': 'runBatteryChargeRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.cpu.runCacheRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.MEMORY_AND_CPU,
'chromeOsRoutineFunction': 'runCpuCacheRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.cpu.runStressRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.MEMORY_AND_CPU,
'chromeOsRoutineFunction': 'runCpuStressRoutine',
},
{
'dpslRoutineFunction':
dpsl.diagnostics.cpu.runFloatingPointAccuracyRoutine,
dpsl.diagnostics.cpu.runFloatingPointAccuracyRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.MEMORY_AND_CPU,
'chromeOsRoutineFunction': 'runCpuFloatingPointAccuracyRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.cpu.runPrimeSearchRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.MEMORY_AND_CPU,
'chromeOsRoutineFunction': 'runCpuPrimeSearchRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.memory.runMemoryRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.MEMORY_AND_CPU,
'chromeOsRoutineFunction': 'runMemoryRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.disk.runReadRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.STORAGE,
'chromeOsRoutineFunction': 'runDiskReadRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.emmc.runEmmcLifetimeRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.STORAGE,
'chromeOsRoutineFunction': 'runEmmcLifetimeRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.nvme.runSmartctlCheckRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.STORAGE,
'chromeOsRoutineFunction': 'runSmartctlCheckRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.nvme.runSelfTestRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.STORAGE,
'chromeOsRoutineFunction': 'runNvmeSelfTestRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.nvme.runWearLevelRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.STORAGE,
'chromeOsRoutineFunction': 'runNvmeWearLevelRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.ufs.runUfsLifetimeRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.STORAGE,
'chromeOsRoutineFunction': 'runUfsLifetimeRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.network
.runDnsResolverPresentRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.NETWORK,
'chromeOsRoutineFunction': 'runDnsResolverPresentRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.network
.runDnsResolutionRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.NETWORK,
'chromeOsRoutineFunction': 'runDnsResolutionRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.network
.runGatewayCanBePingedRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.NETWORK,
'chromeOsRoutineFunction': 'runGatewayCanBePingedRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.network.runLanConnectivityRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.NETWORK,
'chromeOsRoutineFunction': 'runLanConnectivityRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.network.runSignalStrengthRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.NETWORK,
'chromeOsRoutineFunction': 'runSignalStrengthRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.sensor.runSensitiveSensorRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.SENSOR,
'chromeOsRoutineFunction': 'runSensitiveSensorRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.sensor.runFingerprintAliveRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.FINGERPRINT,
'chromeOsRoutineFunction': 'runFingerprintAliveRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.audio.runAudioDriverRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.AUDIO,
'chromeOsRoutineFunction': 'runAudioDriverRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.hardwareButton
.runPowerButtonRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.HARDWARE_BUTTON,
'chromeOsRoutineFunction': 'runPowerButtonRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.bluetooth
.runBluetoothPowerRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.BLUETOOTH,
'chromeOsRoutineFunction': 'runBluetoothPowerRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.bluetooth
.runBluetoothDiscoveryRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.BLUETOOTH,
'chromeOsRoutineFunction': 'runBluetoothDiscoveryRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.bluetooth
.runBluetoothScanningRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.BLUETOOTH,
'chromeOsRoutineFunction': 'runBluetoothScanningRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.bluetooth
.runBluetoothPairingRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.BLUETOOTH,
'chromeOsRoutineFunction': 'runBluetoothPairingRoutine',
},
{
'dpslRoutineFunction': dpsl.diagnostics.fan.runFanRoutine,
'statusCodeCategory': STATUS_CODE_CATEGORY.FAN,
'chromeOsRoutineFunction': 'runFanRoutine',
},
];

testCases.forEach((testCase) => {
test(`${testCase.dpslRoutineFunction}() returns correct data`, (done) => {
// Mock the global chrome object.
const expectedRunRoutineResponse = {id: 123456};
const expectedRunRoutineResponse = {
id: 123456,
category: testCase.statusCodeCategory,
};
const chrome = {
os: {
diagnostics: {
[testCase.chromeOsRoutineFunction]:
() => Promise.resolve(expectedRunRoutineResponse),
() => Promise.resolve(expectedRunRoutineResponse),
},
},
};
Expand All @@ -712,16 +868,18 @@ describe('dpsl.diagnostics tests', () => {


test('runSmartctlCheckRoutine works without parameters', (done) => {
const expectedRunRoutineResponse = {id: 123456};

const expectedRunRoutineResponse = {
id: 123456,
category: STATUS_CODE_CATEGORY.STORAGE,
};
const chrome = {
os: {
diagnostics: {
runSmartctlCheckRoutine:
(params = undefined) => {
return Promise.resolve(params === undefined ?
expectedRunRoutineResponse : undefined);
},
(params = undefined) => {
return Promise.resolve(params === undefined ?
expectedRunRoutineResponse : undefined);
},
},
},
};
Expand All @@ -734,14 +892,16 @@ describe('dpsl.diagnostics tests', () => {
});

test('runSmartctlCheckRoutine works with parameters', (done) => {
const expectedRunRoutineResponse = {id: 123456};

const expectedRunRoutineResponse = {
id: 123456,
category: STATUS_CODE_CATEGORY.STORAGE,
};
const chrome = {
os: {
diagnostics: {
runSmartctlCheckRoutine:
(params = undefined) =>
Promise.resolve(params ? expectedRunRoutineResponse : undefined),
(params = undefined) =>
Promise.resolve(params ? expectedRunRoutineResponse : undefined),
},
},
};
Expand Down
Loading

0 comments on commit 77d25a2

Please sign in to comment.