forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.test.ts
77 lines (68 loc) · 3.15 KB
/
markdown.test.ts
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
import MarkdownNode, { markdown_node_data } from '$lib/compute/markdown_v0';
import deepCopy from 'deep-copy';
import { describe, it, expect } from 'vitest';
describe('MarkdownNode class', () => {
// Existing test in the prompt
it('should set markdown data if input is a string', async () => {
const node = new MarkdownNode(deepCopy(markdown_node_data));
const inputData = { text: '## Title' };
const result = await node.compute(
inputData,
'run',
console.log,
console.error,
console.log,
'/'
);
expect(result).toBe('## Title');
expect(node.data.markdown).toBe('## Title');
expect(node.data.dirty).toBe(false);
});
// Additional Tests Below
it('should combine multiple string inputs with separation', async () => {
const node = new MarkdownNode(deepCopy(markdown_node_data));
const inputData = { text: 'Paragraph 1', additionalText: 'Paragraph 2' };
await node.compute(inputData, 'run', console.log, console.error, console.log, '/');
expect(node.data.markdown).toBe('Paragraph 1\n\nParagraph 2');
});
it('should wrap non-string inputs within code block', async () => {
const node = new MarkdownNode(deepCopy(markdown_node_data));
const inputData = { object: { key: 'value' } };
const expectedCodeBlock = '```\n{\n "key": "value"\n}\n```';
await node.compute(inputData, 'run', console.log, console.error, console.log, '/');
expect(node.data.markdown).toBe(expectedCodeBlock);
});
it('should handle an empty input object', async () => {
const node = new MarkdownNode(deepCopy(markdown_node_data));
const inputData = {};
await node.compute(inputData, 'run', console.log, console.error, console.log, '/');
expect(node.data.markdown).toBe('');
});
it('should preserve the order of inputs when combining', async () => {
const node = new MarkdownNode(deepCopy(markdown_node_data));
const inputData = { first: '# Heading', second: 'Some text.', third: '- List item' };
const combinedContent = '# Heading\n\nSome text.\n\n- List item';
await node.compute(inputData, 'run', console.log, console.error, console.log, '/');
expect(node.data.markdown).toBe(combinedContent);
});
it('should stringify and wrap arrays in code blocks', async () => {
const node = new MarkdownNode(deepCopy(markdown_node_data));
const inputData = { list: ['item1', 'item2'] };
const expectedCodeBlock = '```\n[\n "item1",\n "item2"\n]\n```';
await node.compute(inputData, 'run', console.log, console.error, console.log, '/');
expect(node.data.markdown).toBe(expectedCodeBlock);
});
it('should throw an error if input data contains circular references', async () => {
const node = new MarkdownNode(deepCopy(markdown_node_data));
const inputData = { selfRef: null };
inputData.selfRef = inputData; // Making a circular reference
let hasErrorOccurred = false;
try {
await node.compute(inputData, 'run', console.log, console.error, console.log, '/');
} catch (error) {
hasErrorOccurred = true;
expect(error.message).toContain('Converting circular structure to JSON');
}
expect(hasErrorOccurred).toBe(true);
});
});