|
| 1 | +import { createTestProject, exists, readProjectConfig, runCLI } from '../src/test-utils/index.js'; |
| 2 | +import type { TestProject } from '../src/test-utils/index.js'; |
| 3 | +import { readFile } from 'node:fs/promises'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +async function readHarnessSpec(projectPath: string, harnessName: string) { |
| 8 | + return JSON.parse(await readFile(join(projectPath, `app/${harnessName}/harness.json`), 'utf-8')); |
| 9 | +} |
| 10 | + |
| 11 | +describe('integration: harness add/remove lifecycle', () => { |
| 12 | + let project: TestProject; |
| 13 | + const harnessName = 'TestHarness'; |
| 14 | + |
| 15 | + beforeAll(async () => { |
| 16 | + project = await createTestProject({ noAgent: true }); |
| 17 | + }); |
| 18 | + |
| 19 | + afterAll(async () => { |
| 20 | + await project.cleanup(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('adds a harness with defaults', async () => { |
| 24 | + const result = await runCLI(['add', 'harness', '--name', harnessName, '--json'], project.projectPath); |
| 25 | + |
| 26 | + expect(result.exitCode, `stdout: ${result.stdout}, stderr: ${result.stderr}`).toBe(0); |
| 27 | + const json = JSON.parse(result.stdout); |
| 28 | + expect(json.success).toBe(true); |
| 29 | + |
| 30 | + const config = await readProjectConfig(project.projectPath); |
| 31 | + const harness = config.harnesses?.find((h: { name: string }) => h.name === harnessName); |
| 32 | + expect(harness, `Harness "${harnessName}" should be in agentcore.json`).toBeTruthy(); |
| 33 | + expect(harness!.path).toBe(`app/${harnessName}`); |
| 34 | + }); |
| 35 | + |
| 36 | + it('creates harness.json with correct model config', async () => { |
| 37 | + const spec = await readHarnessSpec(project.projectPath, harnessName); |
| 38 | + expect(spec.model).toBeDefined(); |
| 39 | + expect(spec.model.provider).toBe('bedrock'); |
| 40 | + expect(spec.model.modelId).toBeTruthy(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('creates system-prompt.md', async () => { |
| 44 | + const promptPath = join(project.projectPath, `app/${harnessName}/system-prompt.md`); |
| 45 | + expect(await exists(promptPath), 'system-prompt.md should exist').toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('auto-creates memory resource', async () => { |
| 49 | + const config = await readProjectConfig(project.projectPath); |
| 50 | + const memories = config.memories ?? []; |
| 51 | + expect(memories.length, 'Should have auto-created memory').toBeGreaterThan(0); |
| 52 | + }); |
| 53 | + |
| 54 | + it('rejects duplicate harness name', async () => { |
| 55 | + const result = await runCLI(['add', 'harness', '--name', harnessName, '--json'], project.projectPath); |
| 56 | + expect(result.exitCode).not.toBe(0); |
| 57 | + }); |
| 58 | + |
| 59 | + it('removes the harness', async () => { |
| 60 | + const result = await runCLI(['remove', 'harness', '--name', harnessName, '--json'], project.projectPath); |
| 61 | + |
| 62 | + expect(result.exitCode, `stdout: ${result.stdout}, stderr: ${result.stderr}`).toBe(0); |
| 63 | + const json = JSON.parse(result.stdout); |
| 64 | + expect(json.success).toBe(true); |
| 65 | + |
| 66 | + const config = await readProjectConfig(project.projectPath); |
| 67 | + const found = config.harnesses?.find((h: { name: string }) => h.name === harnessName); |
| 68 | + expect(found, `Harness "${harnessName}" should be removed`).toBeFalsy(); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('integration: harness configuration options', () => { |
| 73 | + let project: TestProject; |
| 74 | + |
| 75 | + beforeAll(async () => { |
| 76 | + project = await createTestProject({ noAgent: true }); |
| 77 | + }); |
| 78 | + |
| 79 | + afterAll(async () => { |
| 80 | + await project.cleanup(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('adds harness with truncation strategy', async () => { |
| 84 | + const name = 'TruncHarness'; |
| 85 | + const result = await runCLI( |
| 86 | + ['add', 'harness', '--name', name, '--truncation-strategy', 'sliding_window', '--json'], |
| 87 | + project.projectPath |
| 88 | + ); |
| 89 | + |
| 90 | + expect(result.exitCode, `stdout: ${result.stdout}, stderr: ${result.stderr}`).toBe(0); |
| 91 | + |
| 92 | + const spec = await readHarnessSpec(project.projectPath, name); |
| 93 | + expect(spec.truncation?.strategy).toBe('sliding_window'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('adds harness with lifecycle config', async () => { |
| 97 | + const name = 'LifecycleHarness'; |
| 98 | + const result = await runCLI( |
| 99 | + ['add', 'harness', '--name', name, '--idle-timeout', '300', '--max-lifetime', '3600', '--json'], |
| 100 | + project.projectPath |
| 101 | + ); |
| 102 | + |
| 103 | + expect(result.exitCode, `stdout: ${result.stdout}, stderr: ${result.stderr}`).toBe(0); |
| 104 | + |
| 105 | + const spec = await readHarnessSpec(project.projectPath, name); |
| 106 | + expect(spec.lifecycleConfig?.idleRuntimeSessionTimeout).toBe(300); |
| 107 | + expect(spec.lifecycleConfig?.maxLifetime).toBe(3600); |
| 108 | + }); |
| 109 | + |
| 110 | + it('adds harness without memory when --no-memory is set', async () => { |
| 111 | + const name = 'NoMemHarness'; |
| 112 | + const configBefore = await readProjectConfig(project.projectPath); |
| 113 | + const memoriesBefore = (configBefore.memories ?? []).length; |
| 114 | + |
| 115 | + const result = await runCLI(['add', 'harness', '--name', name, '--no-memory', '--json'], project.projectPath); |
| 116 | + |
| 117 | + expect(result.exitCode, `stdout: ${result.stdout}, stderr: ${result.stderr}`).toBe(0); |
| 118 | + |
| 119 | + const configAfter = await readProjectConfig(project.projectPath); |
| 120 | + const memoriesAfter = (configAfter.memories ?? []).length; |
| 121 | + expect(memoriesAfter).toBe(memoriesBefore); |
| 122 | + }); |
| 123 | + |
| 124 | + it('adds harness with non-bedrock model provider', async () => { |
| 125 | + const name = 'OpenAIHarness'; |
| 126 | + const result = await runCLI( |
| 127 | + [ |
| 128 | + 'add', |
| 129 | + 'harness', |
| 130 | + '--name', |
| 131 | + name, |
| 132 | + '--model-provider', |
| 133 | + 'open_ai', |
| 134 | + '--model-id', |
| 135 | + 'gpt-5', |
| 136 | + '--api-key-arn', |
| 137 | + 'arn:aws:secretsmanager:us-east-1:123456789012:secret:openai-key', |
| 138 | + '--json', |
| 139 | + ], |
| 140 | + project.projectPath |
| 141 | + ); |
| 142 | + |
| 143 | + expect(result.exitCode, `stdout: ${result.stdout}, stderr: ${result.stderr}`).toBe(0); |
| 144 | + |
| 145 | + const spec = await readHarnessSpec(project.projectPath, name); |
| 146 | + expect(spec.model.provider).toBe('open_ai'); |
| 147 | + expect(spec.model.modelId).toBe('gpt-5'); |
| 148 | + expect(spec.model.apiKeyArn).toBe('arn:aws:secretsmanager:us-east-1:123456789012:secret:openai-key'); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +describe('integration: harness validation errors', () => { |
| 153 | + let project: TestProject; |
| 154 | + |
| 155 | + beforeAll(async () => { |
| 156 | + project = await createTestProject({ noAgent: true }); |
| 157 | + }); |
| 158 | + |
| 159 | + afterAll(async () => { |
| 160 | + await project.cleanup(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('rejects invalid harness name with special characters', async () => { |
| 164 | + const result = await runCLI(['add', 'harness', '--name', 'bad-name!', '--json'], project.projectPath); |
| 165 | + expect(result.exitCode).not.toBe(0); |
| 166 | + }); |
| 167 | + |
| 168 | + it('rejects harness name starting with a number', async () => { |
| 169 | + const result = await runCLI(['add', 'harness', '--name', '1BadName', '--json'], project.projectPath); |
| 170 | + expect(result.exitCode).not.toBe(0); |
| 171 | + }); |
| 172 | + |
| 173 | + it('rejects add harness without --name when --json is passed', async () => { |
| 174 | + const result = await runCLI(['add', 'harness', '--json'], project.projectPath); |
| 175 | + expect(result.exitCode).not.toBe(0); |
| 176 | + }); |
| 177 | +}); |
| 178 | + |
| 179 | +describe('integration: create project with harness', () => { |
| 180 | + let project: TestProject; |
| 181 | + const harnessName = 'CreateHarness'; |
| 182 | + |
| 183 | + beforeAll(async () => { |
| 184 | + project = await createTestProject({ name: harnessName, noAgent: true }); |
| 185 | + await runCLI(['add', 'harness', '--name', harnessName, '--json'], project.projectPath); |
| 186 | + }); |
| 187 | + |
| 188 | + afterAll(async () => { |
| 189 | + await project.cleanup(); |
| 190 | + }); |
| 191 | + |
| 192 | + it('has correct project scaffolding', async () => { |
| 193 | + expect(await exists(join(project.projectPath, 'agentcore/agentcore.json'))).toBe(true); |
| 194 | + expect(await exists(join(project.projectPath, 'agentcore/cdk'))).toBe(true); |
| 195 | + expect(await exists(join(project.projectPath, `app/${harnessName}/harness.json`))).toBe(true); |
| 196 | + expect(await exists(join(project.projectPath, `app/${harnessName}/system-prompt.md`))).toBe(true); |
| 197 | + }); |
| 198 | + |
| 199 | + it('has harness registered in project config', async () => { |
| 200 | + const config = await readProjectConfig(project.projectPath); |
| 201 | + const harness = config.harnesses?.find((h: { name: string }) => h.name === harnessName); |
| 202 | + expect(harness).toBeTruthy(); |
| 203 | + }); |
| 204 | +}); |
0 commit comments