forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocument.test.ts
More file actions
109 lines (80 loc) · 3.41 KB
/
Copy pathdocument.test.ts
File metadata and controls
109 lines (80 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { loadSchemaDocument, saveSchemaDocument } from '../document.js';
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { z } from 'zod';
const TestSchema = z.object({
name: z.string(),
value: z.number(),
});
describe('loadSchemaDocument', () => {
let dir: string;
beforeAll(() => {
dir = mkdtempSync(join(tmpdir(), 'doc-load-'));
});
afterAll(() => {
rmSync(dir, { recursive: true, force: true });
});
it('loads valid JSON with no validation error', async () => {
const filePath = join(dir, 'valid.json');
writeFileSync(filePath, JSON.stringify({ name: 'test', value: 42 }));
const result = await loadSchemaDocument(filePath, TestSchema);
expect(result.validationError).toBeUndefined();
expect(JSON.parse(result.content)).toEqual({ name: 'test', value: 42 });
});
it('returns validation error for schema mismatch', async () => {
const filePath = join(dir, 'bad-schema.json');
writeFileSync(filePath, JSON.stringify({ name: 'test', value: 'not-a-number' }));
const result = await loadSchemaDocument(filePath, TestSchema);
expect(result.validationError).toBeDefined();
expect(result.content).toBeDefined();
});
it('returns validation error for invalid JSON', async () => {
const filePath = join(dir, 'bad-json.json');
writeFileSync(filePath, '{not valid json}');
const result = await loadSchemaDocument(filePath, TestSchema);
expect(result.validationError).toBeDefined();
});
it('throws when file does not exist', async () => {
await expect(loadSchemaDocument(join(dir, 'missing.json'), TestSchema)).rejects.toThrow();
});
});
describe('saveSchemaDocument', () => {
let dir: string;
beforeAll(() => {
dir = mkdtempSync(join(tmpdir(), 'doc-save-'));
});
afterAll(() => {
rmSync(dir, { recursive: true, force: true });
});
it('saves valid JSON and returns formatted content', async () => {
const filePath = join(dir, 'save-valid.json');
const content = JSON.stringify({ name: 'hello', value: 7 });
const result = await saveSchemaDocument(filePath, content, TestSchema);
expect(result.ok).toBe(true);
expect(result.content).toBe(JSON.stringify({ name: 'hello', value: 7 }, null, 2));
expect(readFileSync(filePath, 'utf-8')).toBe(result.content);
});
it('returns error for invalid JSON', async () => {
const filePath = join(dir, 'save-bad-json.json');
const result = await saveSchemaDocument(filePath, '{bad}', TestSchema);
expect(result.ok).toBe(false);
expect(result.error).toBeDefined();
});
it('returns error for schema validation failure', async () => {
const filePath = join(dir, 'save-bad-schema.json');
const content = JSON.stringify({ name: 123, value: 'wrong' });
const result = await saveSchemaDocument(filePath, content, TestSchema);
expect(result.ok).toBe(false);
expect(result.error).toBeDefined();
});
it('returns error when write fails', async () => {
// Try to write to a path under a non-existent directory
const filePath = join(dir, 'no-such-dir', 'nested', 'file.json');
const content = JSON.stringify({ name: 'test', value: 1 });
const result = await saveSchemaDocument(filePath, content, TestSchema);
expect(result.ok).toBe(false);
expect(result.error).toBeDefined();
});
});