|
| 1 | +import { createTestProject, 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 | +describe('integration: tag command', () => { |
| 8 | + let project: TestProject; |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + project = await createTestProject({ |
| 12 | + language: 'Python', |
| 13 | + framework: 'Strands', |
| 14 | + modelProvider: 'Bedrock', |
| 15 | + memory: 'none', |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + afterAll(async () => { |
| 20 | + await project.cleanup(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('creates project with auto-tags in agentcore.json', async () => { |
| 24 | + const specPath = join(project.projectPath, 'agentcore', 'agentcore.json'); |
| 25 | + const spec = JSON.parse(await readFile(specPath, 'utf-8')); |
| 26 | + expect(spec.tags).toEqual({ |
| 27 | + 'agentcore:created-by': 'agentcore-cli', |
| 28 | + 'agentcore:project-name': expect.any(String), |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('set-defaults adds a project-level tag', async () => { |
| 33 | + const result = await runCLI( |
| 34 | + ['tag', 'set-defaults', '--key', 'environment', '--value', 'dev', '--json'], |
| 35 | + project.projectPath |
| 36 | + ); |
| 37 | + expect(result.exitCode, `stderr: ${result.stderr}`).toBe(0); |
| 38 | + const output = JSON.parse(result.stdout); |
| 39 | + expect(output.success).toBe(true); |
| 40 | + |
| 41 | + // Verify in agentcore.json |
| 42 | + const specPath = join(project.projectPath, 'agentcore', 'agentcore.json'); |
| 43 | + const spec = JSON.parse(await readFile(specPath, 'utf-8')); |
| 44 | + expect(spec.tags.environment).toBe('dev'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('tag add sets a per-resource tag', async () => { |
| 48 | + // Get the agent name from spec |
| 49 | + const specPath = join(project.projectPath, 'agentcore', 'agentcore.json'); |
| 50 | + const spec = JSON.parse(await readFile(specPath, 'utf-8')); |
| 51 | + const agentName = spec.agents[0]?.name; |
| 52 | + if (!agentName) return; // Skip if no agent |
| 53 | + |
| 54 | + const result = await runCLI( |
| 55 | + ['tag', 'add', '--resource', `agent:${agentName}`, '--key', 'cost-center', '--value', '12345', '--json'], |
| 56 | + project.projectPath |
| 57 | + ); |
| 58 | + expect(result.exitCode, `stderr: ${result.stderr}`).toBe(0); |
| 59 | + |
| 60 | + // Verify in agentcore.json |
| 61 | + const updatedSpec = JSON.parse(await readFile(specPath, 'utf-8')); |
| 62 | + expect(updatedSpec.agents[0].tags).toEqual({ 'cost-center': '12345' }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('tag list returns JSON output', async () => { |
| 66 | + const result = await runCLI(['tag', 'list', '--json'], project.projectPath); |
| 67 | + expect(result.exitCode, `stderr: ${result.stderr}`).toBe(0); |
| 68 | + const output = JSON.parse(result.stdout); |
| 69 | + expect(output.projectDefaults).toBeDefined(); |
| 70 | + expect(output.resources).toBeInstanceOf(Array); |
| 71 | + }); |
| 72 | + |
| 73 | + it('tag remove removes a per-resource tag', async () => { |
| 74 | + const specPath = join(project.projectPath, 'agentcore', 'agentcore.json'); |
| 75 | + const spec = JSON.parse(await readFile(specPath, 'utf-8')); |
| 76 | + const agentName = spec.agents[0]?.name; |
| 77 | + if (!agentName || !spec.agents[0].tags?.['cost-center']) return; |
| 78 | + |
| 79 | + const result = await runCLI( |
| 80 | + ['tag', 'remove', '--resource', `agent:${agentName}`, '--key', 'cost-center', '--json'], |
| 81 | + project.projectPath |
| 82 | + ); |
| 83 | + expect(result.exitCode, `stderr: ${result.stderr}`).toBe(0); |
| 84 | + |
| 85 | + const updatedSpec = JSON.parse(await readFile(specPath, 'utf-8')); |
| 86 | + expect(updatedSpec.agents[0].tags).toBeUndefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('remove-defaults removes a project-level tag', async () => { |
| 90 | + const result = await runCLI(['tag', 'remove-defaults', '--key', 'environment', '--json'], project.projectPath); |
| 91 | + expect(result.exitCode, `stderr: ${result.stderr}`).toBe(0); |
| 92 | + |
| 93 | + const specPath = join(project.projectPath, 'agentcore', 'agentcore.json'); |
| 94 | + const spec = JSON.parse(await readFile(specPath, 'utf-8')); |
| 95 | + expect(spec.tags.environment).toBeUndefined(); |
| 96 | + }); |
| 97 | +}); |
0 commit comments