|
1 | | -import { describe, expect, it } from 'vitest'; |
2 | | -import { removePairedDevice, toPairedDeviceViews, type PairingState } from './pairing-store'; |
| 1 | +import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { JsonPairingStore, removePairedDevice, toPairedDeviceViews, type PairingState } from './pairing-store'; |
3 | 6 |
|
4 | 7 | describe('toPairedDeviceViews', () => { |
5 | 8 | it('removes shared tokens from paired device metadata', () => { |
@@ -60,6 +63,142 @@ describe('removePairedDevice', () => { |
60 | 63 | }); |
61 | 64 | }); |
62 | 65 |
|
| 66 | +describe('JsonPairingStore', () => { |
| 67 | + let tempDir: string | null = null; |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + vi.restoreAllMocks(); |
| 71 | + if (tempDir) { |
| 72 | + rmSync(tempDir, { recursive: true, force: true }); |
| 73 | + tempDir = null; |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it('creates and saves fresh pairing state when the file is missing', async () => { |
| 78 | + const filePath = pairingPath(); |
| 79 | + |
| 80 | + const state = await new JsonPairingStore(filePath).load(); |
| 81 | + |
| 82 | + expect(state.desktopId).toMatch(/[0-9a-f-]{36}/); |
| 83 | + expect(state.pairedDevices).toEqual([]); |
| 84 | + expect(JSON.parse(readFileSync(filePath, 'utf8'))).toEqual(state); |
| 85 | + }); |
| 86 | + |
| 87 | + it('loads valid pairing state', async () => { |
| 88 | + const filePath = pairingPath(); |
| 89 | + writeFileSync(filePath, JSON.stringify(createState()), 'utf8'); |
| 90 | + |
| 91 | + await expect(new JsonPairingStore(filePath).load()).resolves.toEqual(createState()); |
| 92 | + }); |
| 93 | + |
| 94 | + it('saves formatted JSON and creates parent directories', async () => { |
| 95 | + const filePath = nestedPairingPath(); |
| 96 | + |
| 97 | + await new JsonPairingStore(filePath).save(createState()); |
| 98 | + |
| 99 | + expect(readFileSync(filePath, 'utf8')).toBe(`${JSON.stringify(createState(), null, 2)}\n`); |
| 100 | + }); |
| 101 | + |
| 102 | + it('leaves no temp files after a successful save', async () => { |
| 103 | + const filePath = pairingPath(); |
| 104 | + |
| 105 | + await new JsonPairingStore(filePath).save(createState()); |
| 106 | + |
| 107 | + expect(readdirSync(tempDir!).filter((name) => name.endsWith('.tmp'))).toEqual([]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('backs up invalid JSON and replaces it with fresh valid state', async () => { |
| 111 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined); |
| 112 | + const filePath = pairingPath(); |
| 113 | + writeFileSync(filePath, '{', 'utf8'); |
| 114 | + |
| 115 | + const state = await new JsonPairingStore(filePath).load(); |
| 116 | + |
| 117 | + expect(state.pairedDevices).toEqual([]); |
| 118 | + expect(JSON.parse(readFileSync(filePath, 'utf8'))).toEqual(state); |
| 119 | + expect(corruptBackups()).toHaveLength(1); |
| 120 | + expect(warn.mock.calls.flat().join('\n')).not.toContain('{'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('backs up NUL-byte files and replaces them with fresh valid state', async () => { |
| 124 | + const filePath = pairingPath(); |
| 125 | + writeFileSync(filePath, '\0'.repeat(562), 'utf8'); |
| 126 | + |
| 127 | + const state = await new JsonPairingStore(filePath).load(); |
| 128 | + |
| 129 | + expect(state.desktopId).toMatch(/[0-9a-f-]{36}/); |
| 130 | + expect(state.pairedDevices).toEqual([]); |
| 131 | + expect(readFileSync(filePath, 'utf8')).toContain('"pairedDevices": []'); |
| 132 | + expect(corruptBackups()).toHaveLength(1); |
| 133 | + }); |
| 134 | + |
| 135 | + it('backs up invalid pairing state schema and replaces it with fresh valid state', async () => { |
| 136 | + const filePath = pairingPath(); |
| 137 | + writeFileSync(filePath, JSON.stringify({ desktopId: 1, pairedDevices: [] }), 'utf8'); |
| 138 | + |
| 139 | + const state = await new JsonPairingStore(filePath).load(); |
| 140 | + |
| 141 | + expect(state.pairedDevices).toEqual([]); |
| 142 | + expect(corruptBackups()).toHaveLength(1); |
| 143 | + }); |
| 144 | + |
| 145 | + it('backs up invalid paired-device entries and replaces them with fresh valid state', async () => { |
| 146 | + const filePath = pairingPath(); |
| 147 | + writeFileSync( |
| 148 | + filePath, |
| 149 | + JSON.stringify({ |
| 150 | + desktopId: 'desktop-1', |
| 151 | + pairedDevices: [{ deviceId: 'android-1', token: 'secret-token' }] |
| 152 | + }), |
| 153 | + 'utf8' |
| 154 | + ); |
| 155 | + |
| 156 | + const state = await new JsonPairingStore(filePath).load(); |
| 157 | + |
| 158 | + expect(state.pairedDevices).toEqual([]); |
| 159 | + expect(corruptBackups()).toHaveLength(1); |
| 160 | + }); |
| 161 | + |
| 162 | + it('does not log tokens or corrupt pairing contents during recovery', async () => { |
| 163 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined); |
| 164 | + const filePath = pairingPath(); |
| 165 | + writeFileSync( |
| 166 | + filePath, |
| 167 | + JSON.stringify({ |
| 168 | + desktopId: 'desktop-1', |
| 169 | + pairedDevices: [{ deviceId: 'android-1', token: 'secret-token' }] |
| 170 | + }), |
| 171 | + 'utf8' |
| 172 | + ); |
| 173 | + |
| 174 | + await new JsonPairingStore(filePath).load(); |
| 175 | + |
| 176 | + const warningText = warn.mock.calls.flat().join('\n'); |
| 177 | + expect(warningText).not.toContain('secret-token'); |
| 178 | + expect(warningText).not.toContain('android-1'); |
| 179 | + }); |
| 180 | + |
| 181 | + function pairingPath(): string { |
| 182 | + if (!tempDir) { |
| 183 | + tempDir = mkdtempSync(join(tmpdir(), 'switchify-pairing-store-')); |
| 184 | + } |
| 185 | + |
| 186 | + return join(tempDir, 'pairing-state.json'); |
| 187 | + } |
| 188 | + |
| 189 | + function nestedPairingPath(): string { |
| 190 | + if (!tempDir) { |
| 191 | + tempDir = mkdtempSync(join(tmpdir(), 'switchify-pairing-store-')); |
| 192 | + } |
| 193 | + |
| 194 | + return join(tempDir, 'nested', 'pairing-state.json'); |
| 195 | + } |
| 196 | + |
| 197 | + function corruptBackups(): string[] { |
| 198 | + return readdirSync(tempDir!).filter((name) => name.startsWith('pairing-state.corrupt-')); |
| 199 | + } |
| 200 | +}); |
| 201 | + |
63 | 202 | function createState(): PairingState { |
64 | 203 | return { |
65 | 204 | desktopId: 'desktop-1', |
|
0 commit comments