|
| 1 | +const assert = require('node:assert/strict'); |
| 2 | +const fs = require('node:fs'); |
| 3 | +const Module = require('node:module'); |
| 4 | +const path = require('node:path'); |
| 5 | +const ts = require('typescript'); |
| 6 | + |
| 7 | +function loadTypeScript(sourcePath) { |
| 8 | + const source = fs.readFileSync(sourcePath, 'utf8'); |
| 9 | + const compiled = ts.transpileModule(source, { |
| 10 | + compilerOptions: { |
| 11 | + esModuleInterop: true, |
| 12 | + module: ts.ModuleKind.CommonJS, |
| 13 | + target: ts.ScriptTarget.ES2020, |
| 14 | + strict: true, |
| 15 | + }, |
| 16 | + fileName: sourcePath, |
| 17 | + }); |
| 18 | + const loaded = new Module(sourcePath, module); |
| 19 | + loaded.filename = sourcePath; |
| 20 | + loaded.paths = Module._nodeModulePaths(path.dirname(sourcePath)); |
| 21 | + loaded._compile(compiled.outputText, sourcePath); |
| 22 | + return loaded.exports; |
| 23 | +} |
| 24 | + |
| 25 | +const activationPath = path.join(__dirname, '../src/services/wearableActivation.ts'); |
| 26 | +const { |
| 27 | + NEO_ACTIVE_CONTROL_UUID, |
| 28 | + NEO_ACTIVE_VALUE_BASE64, |
| 29 | + WEARABLE_SERVICE_UUID, |
| 30 | + activateWearableAfterConnect, |
| 31 | +} = loadTypeScript(activationPath); |
| 32 | + |
| 33 | +(async () => { |
| 34 | + const writes = []; |
| 35 | + const neoTransport = { |
| 36 | + characteristicsForDevice: async (deviceId, serviceUuid) => { |
| 37 | + assert.equal(deviceId, 'neo-1'); |
| 38 | + assert.equal(serviceUuid, WEARABLE_SERVICE_UUID); |
| 39 | + return [{ |
| 40 | + uuid: NEO_ACTIVE_CONTROL_UUID.toUpperCase(), |
| 41 | + isWritableWithResponse: true, |
| 42 | + }]; |
| 43 | + }, |
| 44 | + writeCharacteristicWithResponseForDevice: async (...args) => { |
| 45 | + writes.push(args); |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + assert.equal( |
| 50 | + await activateWearableAfterConnect(neoTransport, 'neo-1'), |
| 51 | + 'neo_activated', |
| 52 | + ); |
| 53 | + assert.deepEqual(writes, [[ |
| 54 | + 'neo-1', |
| 55 | + WEARABLE_SERVICE_UUID, |
| 56 | + NEO_ACTIVE_CONTROL_UUID, |
| 57 | + NEO_ACTIVE_VALUE_BASE64, |
| 58 | + ]]); |
| 59 | + assert.equal(NEO_ACTIVE_VALUE_BASE64, 'AQ==', 'Neo Active is the single byte 0x01'); |
| 60 | + |
| 61 | + let genericWriteCount = 0; |
| 62 | + const genericTransport = { |
| 63 | + characteristicsForDevice: async () => [{ |
| 64 | + uuid: '19b10001-e8f2-537e-4f6c-d104768a1214', |
| 65 | + isWritableWithResponse: true, |
| 66 | + }], |
| 67 | + writeCharacteristicWithResponseForDevice: async () => { |
| 68 | + genericWriteCount += 1; |
| 69 | + }, |
| 70 | + }; |
| 71 | + assert.equal( |
| 72 | + await activateWearableAfterConnect(genericTransport, 'omi-1'), |
| 73 | + 'not_required', |
| 74 | + ); |
| 75 | + assert.equal(genericWriteCount, 0, 'ordinary OMI devices remain unchanged'); |
| 76 | + |
| 77 | + const invalidNeoTransport = { |
| 78 | + characteristicsForDevice: async () => [{ |
| 79 | + uuid: NEO_ACTIVE_CONTROL_UUID, |
| 80 | + isWritableWithResponse: false, |
| 81 | + }], |
| 82 | + writeCharacteristicWithResponseForDevice: async () => { |
| 83 | + throw new Error('must not attempt an unsupported write'); |
| 84 | + }, |
| 85 | + }; |
| 86 | + await assert.rejects( |
| 87 | + activateWearableAfterConnect(invalidNeoTransport, 'neo-bad'), |
| 88 | + /does not support writes with response/, |
| 89 | + ); |
| 90 | + |
| 91 | + let releaseWrite; |
| 92 | + const delayedWrite = new Promise((resolve) => { |
| 93 | + releaseWrite = resolve; |
| 94 | + }); |
| 95 | + const delayedTransport = { |
| 96 | + characteristicsForDevice: async () => [{ |
| 97 | + uuid: NEO_ACTIVE_CONTROL_UUID, |
| 98 | + isWritableWithResponse: true, |
| 99 | + }], |
| 100 | + writeCharacteristicWithResponseForDevice: async () => delayedWrite, |
| 101 | + }; |
| 102 | + let activationFinished = false; |
| 103 | + const activation = activateWearableAfterConnect(delayedTransport, 'neo-slow') |
| 104 | + .then(() => { activationFinished = true; }); |
| 105 | + await Promise.resolve(); |
| 106 | + await Promise.resolve(); |
| 107 | + assert.equal(activationFinished, false, 'connection readiness must wait for the Active write'); |
| 108 | + releaseWrite(); |
| 109 | + await activation; |
| 110 | + assert.equal(activationFinished, true); |
| 111 | + |
| 112 | + console.log('wearable activation contract tests passed'); |
| 113 | +})().catch((error) => { |
| 114 | + console.error(error); |
| 115 | + process.exitCode = 1; |
| 116 | +}); |
0 commit comments