|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { randomUUID } from 'node:crypto' |
| 3 | +import { it } from 'node:test' |
| 4 | +import { registerPlugin } from '@capacitor/core' |
| 5 | +import { loadMobileModule } from '../../tooling/load-mobile-module.ts' |
| 6 | + |
| 7 | +const account = { |
| 8 | + base_url: 'https://zennotes.org', connected_at: '2026-09-14T12:00:00Z', |
| 9 | + user: { name: 'Test', email: 'test@example.test' }, |
| 10 | + device: { id: 'test-device', name: 'Test phone', platform: 'android' } |
| 11 | +} |
| 12 | +const credential = JSON.stringify({ base_url: account.base_url, token: 'test-only', account }) |
| 13 | + |
| 14 | +async function coldLaunch(saved: Map<string, string>) { |
| 15 | + // Exercise the real Capacitor lazy proxy: concurrent first calls can |
| 16 | + // instantiate separate implementations, each with its own key prefix. |
| 17 | + const secureStorage = registerPlugin(`TestCloudStorage${randomUUID()}`, { |
| 18 | + web: async () => { |
| 19 | + await Promise.resolve() |
| 20 | + return new class { |
| 21 | + prefix = 'capacitor-storage_' |
| 22 | + async setKeyPrefix(prefix: string) { this.prefix = prefix } |
| 23 | + async setSynchronize(_value: boolean) {} |
| 24 | + async setDefaultKeychainAccess(_value: unknown) {} |
| 25 | + async getItem(key: string) { return saved.get(this.prefix + key) ?? null } |
| 26 | + async setItem(key: string, value: string) { saved.set(this.prefix + key, value) } |
| 27 | + async removeItem(key: string) { saved.delete(this.prefix + key) } |
| 28 | + }() |
| 29 | + } |
| 30 | + }) |
| 31 | + const api = await loadMobileModule('./src/bridge/mobile-cloud-auth.ts', { |
| 32 | + '@capacitor/core': { Capacitor: { isNativePlatform: () => true }, CapacitorHttp: {} }, |
| 33 | + '@capacitor/app': { App: { addListener: async () => ({}), getLaunchUrl: async () => null } }, |
| 34 | + '@aparajita/capacitor-secure-storage': { |
| 35 | + SecureStorage: secureStorage, |
| 36 | + KeychainAccess: { whenUnlockedThisDeviceOnly: 'device-only' } |
| 37 | + }, |
| 38 | + './cloud-sync-client': { createCloudSyncClient: () => assert.fail('status must only read storage') } |
| 39 | + }) |
| 40 | + await api.configureMobileCloudAuth('test-version') |
| 41 | + return api |
| 42 | +} |
| 43 | + |
| 44 | +for (const prefix of ['zennotes.cloud.', 'capacitor-storage_']) { |
| 45 | + it(`loads a saved account from ${prefix} on every cold launch`, async () => { |
| 46 | + const saved = new Map([[prefix + 'credential', credential]]) |
| 47 | + for (let launch = 0; launch < 2; launch++) { |
| 48 | + const api = await coldLaunch(saved) |
| 49 | + const statuses = await Promise.all([api.getMobileCloudAccountStatus(), api.getMobileCloudAccountStatus()]) |
| 50 | + assert.deepEqual(statuses, [{ state: 'connected', account }, { state: 'connected', account }]) |
| 51 | + assert.deepEqual([...saved.keys()], ['zennotes.cloud.credential']) |
| 52 | + } |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +it('preserves the canonical account and prevents a legacy credential from returning after logout', async () => { |
| 57 | + const saved = new Map([ |
| 58 | + ['zennotes.cloud.credential', credential], |
| 59 | + ['capacitor-storage_credential', JSON.stringify({ base_url: account.base_url, token: 'old-test-token', account })] |
| 60 | + ]) |
| 61 | + const api = await coldLaunch(saved) |
| 62 | + assert.equal((await api.authenticatedCredential()).token, 'test-only') |
| 63 | + await api.logoutMobileCloudAccount() |
| 64 | + assert.equal(saved.size, 0) |
| 65 | + assert.deepEqual(await (await coldLaunch(saved)).getMobileCloudAccountStatus(), { state: 'disconnected', account: null }) |
| 66 | +}) |
| 67 | + |
| 68 | +it('rejects invalid recovered credentials through the shared auth validator', async () => { |
| 69 | + const invalid = JSON.stringify({ base_url: 'https://wrong.example.test', token: 'test-only', account }) |
| 70 | + const saved = new Map([['capacitor-storage_credential', invalid]]) |
| 71 | + const api = await coldLaunch(saved) |
| 72 | + assert.deepEqual(await api.getMobileCloudAccountStatus(), { state: 'disconnected', account: null }) |
| 73 | + assert.equal(saved.size, 0) |
| 74 | +}) |
| 75 | + |
| 76 | +it('recovers pending sign-in state across cold launches', async () => { |
| 77 | + const pending = JSON.stringify({ |
| 78 | + base_url: account.base_url, state: 'test-state', code_verifier: 'a'.repeat(43), expires_at: '2099-01-01T00:00:00Z' |
| 79 | + }) |
| 80 | + const saved = new Map([['capacitor-storage_pending-auth', pending]]) |
| 81 | + for (let launch = 0; launch < 2; launch++) { |
| 82 | + const api = await coldLaunch(saved) |
| 83 | + assert.deepEqual(await api.getMobileCloudAccountStatus(), { state: 'connecting', account: null }) |
| 84 | + assert.deepEqual([...saved.entries()], [['zennotes.cloud.pending-auth', pending]]) |
| 85 | + } |
| 86 | +}) |
0 commit comments