|
| 1 | +/** |
| 2 | + * A soul's one mapping and its one id: the persona fields the front matter sets, the id every path keys the agent by, |
| 3 | + * and the check that refuses a front matter field of the wrong type. |
| 4 | + */ |
| 5 | +import { describe, expect, it } from 'vitest'; |
| 6 | +import { frontmatterToPersona, parseSoul, personaFieldsOf, soulAgentIdOf, SoulLoadError, validateSoulFrontmatter } from '../SoulLoader.js'; |
| 7 | +import { stableIdOfName } from '../stableId.js'; |
| 8 | + |
| 9 | +describe('personaFieldsOf', () => { |
| 10 | + const frontmatter = { |
| 11 | + name: 'Aria', agentId: 'aria-1', role: 'Support', |
| 12 | + hexaco: { honestyHumility: 0.9, openness: 0.2 }, |
| 13 | + defaultMood: 'calm', allowedMoods: ['calm', 'warm'], |
| 14 | + hardLimits: ['Never name a diagnosis.'], |
| 15 | + reasoningTrace: { maxEntries: 7 }, |
| 16 | + metadata: { team: 'care' }, |
| 17 | + }; |
| 18 | + |
| 19 | + it('sets the fields the mapping sets, and frontmatterToPersona adds only the version and the prompt', () => { |
| 20 | + const fields = personaFieldsOf(frontmatter); |
| 21 | + expect(fields).toMatchObject({ |
| 22 | + id: 'aria-1', name: 'Aria', description: 'Support', |
| 23 | + personalityTraits: { honesty: 0.9, openness: 0.2 }, |
| 24 | + moodAdaptation: { enabled: true, defaultMood: 'calm', allowedMoods: ['calm', 'warm'] }, |
| 25 | + hardLimits: ['Never name a diagnosis.'], |
| 26 | + reasoningTraceConfig: { maxEntries: 7 }, |
| 27 | + metadata: { team: 'care' }, |
| 28 | + }); |
| 29 | + expect(frontmatterToPersona(frontmatter, 'You are Aria.', 'Short sentences.')).toEqual({ |
| 30 | + ...fields, version: '1.0.0', baseSystemPrompt: 'You are Aria.\n\n## Style\n\nShort sentences.', |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('keeps the id of a soul with a name and no agentId as the slug of the name', () => { |
| 35 | + expect(personaFieldsOf({ name: 'Aria Stone' }).id).toBe('aria-stone'); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('soulAgentIdOf', () => { |
| 40 | + it('takes agentId first, as given', () => { |
| 41 | + expect(soulAgentIdOf({ agentId: 'aria-1', name: 'Aria' }, 'Support')).toBe('aria-1'); |
| 42 | + }); |
| 43 | + it('takes the slug of the soul name when there is no agentId, whatever the agent is named', () => { |
| 44 | + expect(soulAgentIdOf({ name: 'Aria Stone' }, 'Support')).toBe('aria-stone'); |
| 45 | + }); |
| 46 | + it("takes the agent's name when the soul has neither, by the rule a named agent's id follows", () => { |
| 47 | + expect(soulAgentIdOf({}, 'Support Desk')).toBe('support-desk'); |
| 48 | + expect(soulAgentIdOf({}, 'Zoë')).toBe(stableIdOfName('Zoë')); |
| 49 | + }); |
| 50 | + it('is undefined when the soul has neither and the agent has no name, and for blank values', () => { |
| 51 | + expect(soulAgentIdOf({})).toBeUndefined(); |
| 52 | + expect(soulAgentIdOf({ agentId: ' ', name: ' ' }, ' ')).toBeUndefined(); |
| 53 | + }); |
| 54 | + it('never answers a shared fallback id', () => { |
| 55 | + for (const id of [soulAgentIdOf({}), soulAgentIdOf({}, undefined)]) expect(id).not.toBe('agent'); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('validateSoulFrontmatter', () => { |
| 60 | + it('returns valid front matter, keeps unknown keys and drops a declared field set to null', () => { |
| 61 | + expect(validateSoulFrontmatter({ name: 'Aria', hardLimits: ['a'], role: null, custom: 1 })).toEqual({ name: 'Aria', hardLimits: ['a'], custom: 1 }); |
| 62 | + }); |
| 63 | + it.each([ |
| 64 | + ['name', 42, 'a string'], |
| 65 | + ['agentId', 7, 'a string'], |
| 66 | + ['role', ['x'], 'a string'], |
| 67 | + ['defaultMood', {}, 'a string'], |
| 68 | + ['allowedMoods', 'calm', 'a list of strings'], |
| 69 | + ['hardLimits', 'never', 'a list of strings'], |
| 70 | + ['hardLimits', ['ok', 3], 'a list of strings'], |
| 71 | + ['voice', 'rachel', 'an object'], |
| 72 | + ['avatar', [], 'an object'], |
| 73 | + ['reasoningTrace', 42, 'an object'], |
| 74 | + ['metadata', 'x', 'an object'], |
| 75 | + ['hexaco', { openness: 'high' }, 'an object of numbers between 0 and 1'], |
| 76 | + ['hexaco', { openness: 1.5 }, 'an object of numbers between 0 and 1'], |
| 77 | + ])('refuses %s of the wrong type, naming the field', (field, value, expected) => { |
| 78 | + let thrown: unknown; |
| 79 | + try { |
| 80 | + validateSoulFrontmatter({ [field]: value }, '/souls/aria'); |
| 81 | + } catch (error) { |
| 82 | + thrown = error; |
| 83 | + } |
| 84 | + expect(thrown).toBeInstanceOf(SoulLoadError); |
| 85 | + expect(thrown).toMatchObject({ name: 'SoulLoadError', code: 'SOUL_LOAD_FAILED', source: '/souls/aria' }); |
| 86 | + expect((thrown as Error).message).toBe(`The soul failed to load (/souls/aria): front matter field '${field}' must be ${expected}`); |
| 87 | + }); |
| 88 | + it('is what parseSoul runs, with the inline source', () => { |
| 89 | + expect(() => parseSoul("---\nhardLimits: never\n---\nYou are Aria.")).toThrow("The soul failed to load (inline soul): front matter field 'hardLimits' must be a list of strings"); |
| 90 | + }); |
| 91 | +}); |
0 commit comments