|
15 | 15 | */ |
16 | 16 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
17 | 17 |
|
18 | | -import { isTauri } from './common'; |
| 18 | +import { isTauri, parseServiceCliOutput } from './common'; |
19 | 19 |
|
20 | 20 | const coreIsTauriMock = vi.fn(); |
21 | 21 |
|
@@ -90,3 +90,64 @@ describe('isTauri (tauriCommands/common)', () => { |
90 | 90 | expect(isTauri()).toBe(false); |
91 | 91 | }); |
92 | 92 | }); |
| 93 | + |
| 94 | +// `parseServiceCliOutput` runs against raw CLI stdout from the `openhuman` |
| 95 | +// sidecar. The core process can crash mid-write, return a partial response, or |
| 96 | +// drift from the expected JSON schema across versions — any of which produces |
| 97 | +// malformed input. The guard must reject those cases with a descriptive error |
| 98 | +// rather than handing typed garbage back to callers. |
| 99 | +describe('parseServiceCliOutput (tauriCommands/common)', () => { |
| 100 | + it('returns the parsed response when shape matches CommandResponse', () => { |
| 101 | + const raw = JSON.stringify({ result: { value: 42 }, logs: ['ok'] }); |
| 102 | + |
| 103 | + const parsed = parseServiceCliOutput<{ value: number }>(raw); |
| 104 | + |
| 105 | + expect(parsed.result).toEqual({ value: 42 }); |
| 106 | + expect(parsed.logs).toEqual(['ok']); |
| 107 | + }); |
| 108 | + |
| 109 | + it('accepts null result and empty logs (valid CommandResponse shape)', () => { |
| 110 | + const raw = JSON.stringify({ result: null, logs: [] }); |
| 111 | + |
| 112 | + const parsed = parseServiceCliOutput<null>(raw); |
| 113 | + |
| 114 | + expect(parsed.result).toBeNull(); |
| 115 | + expect(parsed.logs).toEqual([]); |
| 116 | + }); |
| 117 | + |
| 118 | + it('throws a descriptive error when the input is not valid JSON', () => { |
| 119 | + expect(() => parseServiceCliOutput('not-json')).toThrow(/Failed to parse service CLI output/); |
| 120 | + }); |
| 121 | + |
| 122 | + it('throws when the parsed value is null', () => { |
| 123 | + expect(() => parseServiceCliOutput('null')).toThrow(/CommandResponse shape/); |
| 124 | + }); |
| 125 | + |
| 126 | + it('throws when the parsed value is an array (not an object)', () => { |
| 127 | + expect(() => parseServiceCliOutput('[]')).toThrow(/CommandResponse shape/); |
| 128 | + }); |
| 129 | + |
| 130 | + it('throws when required `logs` field is missing', () => { |
| 131 | + expect(() => parseServiceCliOutput(JSON.stringify({ result: 1 }))).toThrow( |
| 132 | + /CommandResponse shape/ |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + it('throws when required `result` field is missing', () => { |
| 137 | + expect(() => parseServiceCliOutput(JSON.stringify({ logs: [] }))).toThrow( |
| 138 | + /CommandResponse shape/ |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it('throws when `logs` is not an array', () => { |
| 143 | + expect(() => parseServiceCliOutput(JSON.stringify({ result: 1, logs: 'oops' }))).toThrow( |
| 144 | + /CommandResponse shape/ |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it('throws when `logs` contains non-string entries', () => { |
| 149 | + expect(() => parseServiceCliOutput(JSON.stringify({ result: 1, logs: [1, 2] }))).toThrow( |
| 150 | + /CommandResponse shape/ |
| 151 | + ); |
| 152 | + }); |
| 153 | +}); |
0 commit comments