-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
144 lines (118 loc) · 3.65 KB
/
index.test.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { describe, beforeEach, it, expect, vi, afterEach } from "vitest";
import path from "path";
import { fs, Volume } from "memfs";
const vol = new Volume();
vi.mock("node:fs");
vi.mock("node:fs/promises");
describe("jsonSchemaToDiagram", () => {
let jsonSchemaToDiagram;
const startMarker = "<!-- MERMAID_DIAGRAM_START -->";
const endMarker = "<!-- MERMAID_DIAGRAM_END -->";
const resolvedPath = path.resolve(process.cwd(), "./README.md");
const filePath = "./README.md";
beforeEach(async () => {
process.env.OPENAI_API_KEY = "test-api-key";
vi.mock("node:fs/promises", async () => {
const memfs = await vi.importActual("memfs");
return memfs.fs.promises;
});
vol.fromJSON({
resolvedPath:
"<!-- MERMAID_DIAGRAM_START -->\nOld content\n<!-- MERMAID_DIAGRAM_END -->",
});
vi.mock("ai", () => ({
__esModule: true,
generateObject: vi.fn().mockResolvedValue({
object: { mermaidDiagramString: "graph TD; A-->B;" },
}),
}));
const module = await import("./index.js");
jsonSchemaToDiagram = module.default;
});
afterEach(() => {
vol.reset();
vi.restoreAllMocks();
});
it("throws error if OPENAI_API_KEY is not set", async () => {
delete process.env.OPENAI_API_KEY;
await expect(jsonSchemaToDiagram({})).rejects.toThrow(
"OPENAI_API_KEY is not set"
);
});
it("throws error if options is not an object", async () => {
await expect(jsonSchemaToDiagram(null)).rejects.toThrow(
"Expected a non-null object"
);
});
it("throws error if required options are missing", async () => {
await expect(jsonSchemaToDiagram({})).rejects.toThrow(
"Missing required options"
);
});
it.todo("updates file with generated mermaid diagram", async () => {
const systemPrompt = "test system prompt";
const model = "test model";
const jsonSchema = { tools: [] };
const mermaidDiagramString = "graph TD; A-->B;";
vi.spyOn(fs, "writeFile").mockImplementation(() => {
return Promise.resolve();
});
await jsonSchemaToDiagram({
filePath,
startMarker,
endMarker,
systemPrompt,
model,
jsonSchema,
});
const expectedContent = `${startMarker}\n\`\`\`mermaid\n${mermaidDiagramString}\n\`\`\`\n${endMarker}`;
expect(fs.writeFile).toHaveBeenCalledWith(
path.join(__dirname, filePath),
expectedContent,
"utf8"
);
});
it.todo("logs error if markers are not found in file", async () => {
const systemPrompt = "test system prompt";
const model = "test model";
const jsonSchema = { tools: [] };
vol.fromJSON({
resolvedPath: "No markers here",
});
vi.spyOn(console, "error").mockImplementation(() => {});
await jsonSchemaToDiagram({
filePath,
startMarker,
endMarker,
systemPrompt,
model,
jsonSchema,
});
expect(console.error).toHaveBeenCalledWith(
`Markers not found in ${filePath}:`,
expect.any(Error)
);
});
it("logs error if the LLM fails to generate the diagram", async () => {
const systemPrompt = "test system prompt";
const model = "test model";
const jsonSchema = { tools: [] };
vi.spyOn(console, "error").mockImplementation(() => {});
vi.mock("ai", () => ({
__esModule: true,
generateObject: vi.fn().mockRejectedValue(new Error("Processing failed")),
}));
await jsonSchemaToDiagram({
filePath,
startMarker,
endMarker,
systemPrompt,
model,
jsonSchema,
});
expect(console.error).toHaveBeenCalledWith(
`Error processing ${resolvedPath}:`,
expect.any(Error)
);
});
});