|
| 1 | +const test = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | +const fs = require('node:fs'); |
| 4 | +const path = require('node:path'); |
| 5 | +const ts = require('typescript'); |
| 6 | + |
| 7 | +// Exercise the actual shared login action without creating native UI owners. |
| 8 | +const file = path.join(__dirname, '../../entry/src/main/ets/pages/runtime/AppRootRuntimeComposition.ets'); |
| 9 | +const source = fs.readFileSync(file, 'utf8'); |
| 10 | +const ast = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true); |
| 11 | +let initializer; |
| 12 | +function visit(node) { |
| 13 | + if (ts.isPropertyAssignment(node) && node.name.getText(ast) === 'cloudLogin') { |
| 14 | + initializer = node.initializer.getText(ast); |
| 15 | + } |
| 16 | + ts.forEachChild(node, visit); |
| 17 | +} |
| 18 | +visit(ast); |
| 19 | +assert.ok(initializer, 'shared login action exists'); |
| 20 | +const js = ts.transpileModule(`const action = ${initializer};`, { |
| 21 | + compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS }, |
| 22 | +}).outputText; |
| 23 | +function action(context) { |
| 24 | + return new Function(`${js}\nreturn action;`).call(context); |
| 25 | +} |
| 26 | + |
| 27 | +test('foreground restoration before login resolves still starts the watch listener after login', async () => { |
| 28 | + let resolveLogin; |
| 29 | + const calls = []; |
| 30 | + const context = { |
| 31 | + settingsController: { loginCloudAccount: () => new Promise(resolve => { resolveLogin = resolve; }) }, |
| 32 | + startWatchProvisioning: async () => { calls.push('listen'); }, |
| 33 | + pendingAccountDeviceId: '', |
| 34 | + }; |
| 35 | + const result = action(context)(); |
| 36 | + assert.deepEqual(calls, []); |
| 37 | + resolveLogin('account'); |
| 38 | + assert.equal(await result, 'account'); |
| 39 | + assert.deepEqual(calls, ['listen']); |
| 40 | +}); |
| 41 | + |
| 42 | +test('watch listener starts before resuming a pending desktop link', async () => { |
| 43 | + const calls = []; |
| 44 | + const context = { |
| 45 | + settingsController: { loginCloudAccount: async () => 'account' }, |
| 46 | + startWatchProvisioning: async () => { calls.push('listen'); }, |
| 47 | + pendingAccountDeviceId: 'desktop', |
| 48 | + connectAccountDeviceLink: async id => { calls.push(id); }, |
| 49 | + }; |
| 50 | + assert.equal(await action(context)(), 'account'); |
| 51 | + assert.deepEqual(calls, ['listen', 'desktop']); |
| 52 | + assert.equal(context.pendingAccountDeviceId, ''); |
| 53 | +}); |
| 54 | + |
| 55 | +test('cancelled or failed account login does not start provisioning or consume a pending link', async () => { |
| 56 | + let started = false; |
| 57 | + const context = { |
| 58 | + settingsController: { loginCloudAccount: async () => { throw new Error('cancelled'); } }, |
| 59 | + startWatchProvisioning: async () => { started = true; }, |
| 60 | + pendingAccountDeviceId: 'desktop', |
| 61 | + }; |
| 62 | + await assert.rejects(action(context)(), /cancelled/); |
| 63 | + assert.equal(started, false); |
| 64 | + assert.equal(context.pendingAccountDeviceId, 'desktop'); |
| 65 | +}); |
0 commit comments