|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import{createHash}from'node:crypto'; |
| 5 | +import{promisesasfsp}from'node:fs'; |
| 6 | +import{tmpdir}from'node:os'; |
| 7 | +importnodePathfrom'node:path'; |
| 8 | +import{afterEach,describe,expect,it}from'vitest'; |
| 9 | +import{skills,typeSkill}from'./registry.js'; |
| 10 | +import{ |
| 11 | +AGENT_SKILLS_DISCOVERY_SCHEMA, |
| 12 | +createAgentSkillArtifacts, |
| 13 | +validateSkillDescription, |
| 14 | +validateSkillName, |
| 15 | +writeAgentSkillArtifacts |
| 16 | +}from'./utils.js'; |
| 17 | + |
| 18 | +consttemporaryDirectories: string[]=[]; |
| 19 | + |
| 20 | +functioncreateSkill(name: string,overrides: Partial<Skill>={}): Skill{ |
| 21 | +return{ |
| 22 | + name, |
| 23 | +title: `${name} title`, |
| 24 | +description: `${name} description`, |
| 25 | +context: `# ${name}\n\n${name} context`, |
| 26 | + ...overrides |
| 27 | +}; |
| 28 | +} |
| 29 | + |
| 30 | +asyncfunctioncreateTemporaryDirectory(){ |
| 31 | +constdirectory=awaitfsp.mkdtemp(nodePath.join(tmpdir(),'elements-agent-skills-')); |
| 32 | +temporaryDirectories.push(directory); |
| 33 | +returndirectory; |
| 34 | +} |
| 35 | + |
| 36 | +afterEach(async()=>{ |
| 37 | +awaitPromise.all( |
| 38 | +temporaryDirectories.splice(0).map(directory=>fsp.rm(directory,{recursive: true,force: true})) |
| 39 | +); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('validateSkillName',()=>{ |
| 43 | +it.each(['a','elements','skill-1','a'.repeat(64)])('should accept valid name %s',name=>{ |
| 44 | +expect(()=>validateSkillName(name)).not.toThrow(); |
| 45 | +}); |
| 46 | + |
| 47 | +it.each([undefined,null,1,'','Invalid','-invalid','invalid-','invalid--name','invalid_name','a'.repeat(65)])( |
| 48 | +'should reject invalid name %s', |
| 49 | +name=>{ |
| 50 | +expect(()=>validateSkillName(name)).toThrow(/InvalidAgentSkillname/); |
| 51 | +} |
| 52 | +); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('validateSkillDescription',()=>{ |
| 56 | +it.each(['a','a'.repeat(1024)])('should accept a description between 1 and 1,024 characters',description=>{ |
| 57 | +expect(()=>validateSkillDescription('elements',description)).not.toThrow(); |
| 58 | +}); |
| 59 | + |
| 60 | +it.each([undefined,null,1,'',' ','a'.repeat(1025)])('should reject invalid description %s',description=>{ |
| 61 | +expect(()=>validateSkillDescription('elements',description)).toThrow( |
| 62 | +/InvalidAgentSkilldescriptionfor"elements"/ |
| 63 | +); |
| 64 | +}); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('createAgentSkillArtifacts',()=>{ |
| 68 | +it('should create deterministic discovery 0.2 entries',()=>{ |
| 69 | +constregistry=[createSkill('zeta'),createSkill('alpha')]; |
| 70 | +constartifacts=createAgentSkillArtifacts(registry); |
| 71 | + |
| 72 | +expect(artifacts.index.$schema).toBe(AGENT_SKILLS_DISCOVERY_SCHEMA); |
| 73 | +expect(artifacts.index.skills.map(skill=>skill.name)).toEqual(['alpha','zeta']); |
| 74 | +expect(artifacts.index.skills.every(skill=>skill.type==='skill-md')).toBe(true); |
| 75 | +expect(artifacts.index.skills.map(skill=>skill.url)).toEqual(['alpha/SKILL.md','zeta/SKILL.md']); |
| 76 | +expect(createAgentSkillArtifacts([...registry].reverse())).toEqual(artifacts); |
| 77 | +}); |
| 78 | + |
| 79 | +it('should hash the exact generated Markdown bytes',()=>{ |
| 80 | +constartifacts=createAgentSkillArtifacts(); |
| 81 | + |
| 82 | +expect(artifacts.index.skills.map(entry=>entry.name)).toEqual(skills.map(skill=>skill.name).sort()); |
| 83 | +artifacts.index.skills.forEach(entry=>{ |
| 84 | +constmarkdown=artifacts.files.get(entry.url); |
| 85 | +expect(markdown).toBeDefined(); |
| 86 | +if(!markdown)return; |
| 87 | +expect(entry.digest).toBe(`sha256:${createHash('sha256').update(Buffer.from(markdown,'utf8')).digest('hex')}`); |
| 88 | +}); |
| 89 | +}); |
| 90 | + |
| 91 | +it('should generate standard frontmatter with the registry context',()=>{ |
| 92 | +constelements=skills.find(skill=>skill.name==='elements'); |
| 93 | +expect(elements).toBeDefined(); |
| 94 | +if(!elements)return; |
| 95 | + |
| 96 | +constartifacts=createAgentSkillArtifacts([elements]); |
| 97 | +constmarkdown=artifacts.files.get('elements/SKILL.md'); |
| 98 | + |
| 99 | +expect(markdown).toMatch( |
| 100 | +/^---\nname:"elements"\ndescription:".+"\nlicense:"Apache-2.0"\nmetadata:\ntitle:"NVIDIAElementsDesignSystem\(nve\)"\n---\n/ |
| 101 | +); |
| 102 | +expect(markdown).not.toMatch(/^title:/m); |
| 103 | +expect(markdown).toContain(elements.context.trim()); |
| 104 | +expect(markdown?.endsWith('\n')).toBe(true); |
| 105 | +expect(markdown?.endsWith('\n\n')).toBe(false); |
| 106 | +}); |
| 107 | + |
| 108 | +it('should publish conditional skills supplied by the registry',()=>{ |
| 109 | +constartifacts=createAgentSkillArtifacts([createSkill('playground')]); |
| 110 | + |
| 111 | +expect(artifacts.index.skills.map(skill=>skill.name)).toEqual(['playground']); |
| 112 | +expect(artifacts.files.has('playground/SKILL.md')).toBe(true); |
| 113 | +}); |
| 114 | + |
| 115 | +it('should reject duplicate skill names',()=>{ |
| 116 | +expect(()=>createAgentSkillArtifacts([createSkill('duplicate'),createSkill('duplicate')])).toThrow( |
| 117 | +'Duplicate Agent Skill name "duplicate".' |
| 118 | +); |
| 119 | +}); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('writeAgentSkillArtifacts',()=>{ |
| 123 | +it('should write the index and skill directory tree',async()=>{ |
| 124 | +constpublicOutputPath=awaitcreateTemporaryDirectory(); |
| 125 | +awaitwriteAgentSkillArtifacts(publicOutputPath,[createSkill('alpha'),createSkill('beta')]); |
| 126 | +constoutputPath=nodePath.join(publicOutputPath,'.well-known','agent-skills'); |
| 127 | + |
| 128 | +constindex=JSON.parse(awaitfsp.readFile(nodePath.join(outputPath,'index.json'),'utf8')); |
| 129 | +expect(index.skills).toEqual([ |
| 130 | +expect.objectContaining({name: 'alpha'}), |
| 131 | +expect.objectContaining({name: 'beta'}) |
| 132 | +]); |
| 133 | +awaitexpect(fsp.readFile(nodePath.join(outputPath,'alpha','SKILL.md'),'utf8')).resolves.toContain( |
| 134 | +'name: "alpha"' |
| 135 | +); |
| 136 | +awaitexpect(fsp.readFile(nodePath.join(outputPath,'beta','SKILL.md'),'utf8')).resolves.toContain( |
| 137 | +'name: "beta"' |
| 138 | +); |
| 139 | +expect((awaitfsp.readFile(nodePath.join(outputPath,'index.json'),'utf8')).endsWith('\n')).toBe(true); |
| 140 | +}); |
| 141 | + |
| 142 | +it('should remove stale skills before writing',async()=>{ |
| 143 | +constpublicOutputPath=awaitcreateTemporaryDirectory(); |
| 144 | +constoutputPath=nodePath.join(publicOutputPath,'.well-known','agent-skills'); |
| 145 | +awaitwriteAgentSkillArtifacts(publicOutputPath,[createSkill('current'),createSkill('stale')]); |
| 146 | +awaitwriteAgentSkillArtifacts(publicOutputPath,[createSkill('current')]); |
| 147 | + |
| 148 | +awaitexpect(fsp.stat(nodePath.join(outputPath,'stale'))).rejects.toMatchObject({code: 'ENOENT'}); |
| 149 | +awaitexpect(fsp.readFile(nodePath.join(outputPath,'current','SKILL.md'),'utf8')).resolves.toBeDefined(); |
| 150 | +}); |
| 151 | +}); |
0 commit comments