|
| 1 | +import { createTestProject, runCLI } from '../src/test-utils/index.js'; |
| 2 | +import type { TestProject } from '../src/test-utils/index.js'; |
| 3 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +const SCHEMA_URL_PATTERN = /^https:\/\/schema\.agentcore\.aws\.dev\/.+\.json$/; |
| 8 | +async function readRawConfig(projectPath: string): Promise<Record<string, unknown>> { |
| 9 | + const raw = await readFile(join(projectPath, 'agentcore', 'agentcore.json'), 'utf-8'); |
| 10 | + return JSON.parse(raw) as Record<string, unknown>; |
| 11 | +} |
| 12 | + |
| 13 | +describe('integration: $schema injection in agentcore.json', () => { |
| 14 | + let project: TestProject; |
| 15 | + |
| 16 | + beforeAll(async () => { |
| 17 | + project = await createTestProject({ |
| 18 | + language: 'Python', |
| 19 | + framework: 'Strands', |
| 20 | + modelProvider: 'Bedrock', |
| 21 | + memory: 'none', |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + afterAll(async () => { |
| 26 | + await project.cleanup(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('new project has $schema set to the official URL as the first key', async () => { |
| 30 | + const config = await readRawConfig(project.projectPath); |
| 31 | + expect(config.$schema).toMatch(SCHEMA_URL_PATTERN); |
| 32 | + expect(Object.keys(config)[0]).toBe('$schema'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('$schema persists after adding a resource', async () => { |
| 36 | + const memName = `SchemaMem${Date.now().toString().slice(-6)}`; |
| 37 | + await runCLI(['add', 'memory', '--name', memName, '--json'], project.projectPath); |
| 38 | + |
| 39 | + const config = await readRawConfig(project.projectPath); |
| 40 | + expect(config.$schema).toMatch(SCHEMA_URL_PATTERN); |
| 41 | + |
| 42 | + await runCLI(['remove', 'memory', '--name', memName, '--json'], project.projectPath); |
| 43 | + }); |
| 44 | + |
| 45 | + it('does not overwrite a custom $schema value', async () => { |
| 46 | + const configPath = join(project.projectPath, 'agentcore', 'agentcore.json'); |
| 47 | + const config = await readRawConfig(project.projectPath); |
| 48 | + const customUrl = 'https://example.com/custom-schema.json'; |
| 49 | + config.$schema = customUrl; |
| 50 | + await writeFile(configPath, JSON.stringify(config, null, 2)); |
| 51 | + |
| 52 | + const memName = `CustomMem${Date.now().toString().slice(-6)}`; |
| 53 | + await runCLI(['add', 'memory', '--name', memName, '--json'], project.projectPath); |
| 54 | + |
| 55 | + const updated = await readRawConfig(project.projectPath); |
| 56 | + expect(updated.$schema).toBe(customUrl); |
| 57 | + |
| 58 | + await runCLI(['remove', 'memory', '--name', memName, '--json'], project.projectPath); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does not inject $schema into a pre-existing project that lacks one', async () => { |
| 62 | + const configPath = join(project.projectPath, 'agentcore', 'agentcore.json'); |
| 63 | + const config = await readRawConfig(project.projectPath); |
| 64 | + |
| 65 | + // Simulate an old project by stripping $schema |
| 66 | + delete config.$schema; |
| 67 | + await writeFile(configPath, JSON.stringify(config, null, 2)); |
| 68 | + |
| 69 | + // Trigger a write |
| 70 | + const memName = `OldProj${Date.now().toString().slice(-6)}`; |
| 71 | + await runCLI(['add', 'memory', '--name', memName, '--json'], project.projectPath); |
| 72 | + |
| 73 | + const updated = await readRawConfig(project.projectPath); |
| 74 | + expect(updated.$schema).toBeUndefined(); |
| 75 | + |
| 76 | + await runCLI(['remove', 'memory', '--name', memName, '--json'], project.projectPath); |
| 77 | + }); |
| 78 | +}); |
0 commit comments