|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { isBlankInstructions, type SettingField } from '../../src/views/settings/fields' |
| 3 | + |
| 4 | +// MCP-2484: instructions textarea prefill + reset-to-default |
| 5 | + |
| 6 | +// Unit: SettingField interface accepts resetDefault (compile-time test via import) |
| 7 | +describe('SettingField.resetDefault (MCP-2484)', () => { |
| 8 | + it('allows a resetDefault property on SettingField without TS error', () => { |
| 9 | + const f: SettingField = { |
| 10 | + key: 'instructions', |
| 11 | + label: 'Server instructions', |
| 12 | + control: 'textarea', |
| 13 | + optional: true, |
| 14 | + resetDefault: 'built-in default text', |
| 15 | + } |
| 16 | + expect(f.resetDefault).toBe('built-in default text') |
| 17 | + }) |
| 18 | + |
| 19 | + it('resetDefault is optional — omitting it does not cause a TS error', () => { |
| 20 | + const f: SettingField = { |
| 21 | + key: 'instructions', |
| 22 | + label: 'Server instructions', |
| 23 | + control: 'textarea', |
| 24 | + optional: true, |
| 25 | + } |
| 26 | + expect(f.resetDefault).toBeUndefined() |
| 27 | + }) |
| 28 | +}) |
| 29 | + |
| 30 | +// Unit: isBlankInstructions helper |
| 31 | +describe('isBlankInstructions (MCP-2484)', () => { |
| 32 | + it('treats unset/empty/whitespace as blank (eligible for prefill)', () => { |
| 33 | + expect(isBlankInstructions(undefined)).toBe(true) |
| 34 | + expect(isBlankInstructions(null)).toBe(true) |
| 35 | + expect(isBlankInstructions('')).toBe(true) |
| 36 | + expect(isBlankInstructions(' \n ')).toBe(true) |
| 37 | + }) |
| 38 | + |
| 39 | + it('treats a real saved value as non-blank (never overwrite)', () => { |
| 40 | + expect(isBlankInstructions('my custom instructions')).toBe(false) |
| 41 | + }) |
| 42 | +}) |
| 43 | + |
| 44 | +// Unit: maybePrefillInstructions logic (isolated, not mounting the full component) |
| 45 | +describe('maybePrefillInstructions logic (MCP-2484)', () => { |
| 46 | + function prefill(working: Record<string, any>, defaultVal: string, isLoaded: boolean) { |
| 47 | + if (!defaultVal) return |
| 48 | + if (!isLoaded) return |
| 49 | + if (isBlankInstructions(working.instructions)) { |
| 50 | + working.instructions = defaultVal |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + it('prefills instructions when config is empty and default is available', () => { |
| 55 | + const w: Record<string, any> = { instructions: '' } |
| 56 | + prefill(w, 'built-in default', true) |
| 57 | + expect(w.instructions).toBe('built-in default') |
| 58 | + }) |
| 59 | + |
| 60 | + it('prefills when instructions is null', () => { |
| 61 | + const w: Record<string, any> = { instructions: null } |
| 62 | + prefill(w, 'built-in default', true) |
| 63 | + expect(w.instructions).toBe('built-in default') |
| 64 | + }) |
| 65 | + |
| 66 | + it('prefills when instructions is undefined', () => { |
| 67 | + const w: Record<string, any> = {} |
| 68 | + prefill(w, 'built-in default', true) |
| 69 | + expect(w.instructions).toBe('built-in default') |
| 70 | + }) |
| 71 | + |
| 72 | + it('prefills when instructions is whitespace-only', () => { |
| 73 | + const w: Record<string, any> = { instructions: ' \n ' } |
| 74 | + prefill(w, 'built-in default', true) |
| 75 | + expect(w.instructions).toBe('built-in default') |
| 76 | + }) |
| 77 | + |
| 78 | + it('does NOT overwrite a saved custom instruction', () => { |
| 79 | + const w: Record<string, any> = { instructions: 'my custom instructions' } |
| 80 | + prefill(w, 'built-in default', true) |
| 81 | + expect(w.instructions).toBe('my custom instructions') |
| 82 | + }) |
| 83 | + |
| 84 | + it('does NOT prefill when config is not yet loaded', () => { |
| 85 | + const w: Record<string, any> = { instructions: '' } |
| 86 | + prefill(w, 'built-in default', false) |
| 87 | + expect(w.instructions).toBe('') |
| 88 | + }) |
| 89 | + |
| 90 | + it('does NOT prefill when defaultInstructions is empty (API not yet resolved)', () => { |
| 91 | + const w: Record<string, any> = { instructions: '' } |
| 92 | + prefill(w, '', true) |
| 93 | + expect(w.instructions).toBe('') |
| 94 | + }) |
| 95 | +}) |
0 commit comments