-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsolution-blueprint.test.ts
More file actions
191 lines (172 loc) · 6.21 KB
/
Copy pathsolution-blueprint.test.ts
File metadata and controls
191 lines (172 loc) · 6.21 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect } from 'vitest';
import {
SolutionBlueprintSchema,
SolutionBlueprintStrictSchema,
defineSolutionBlueprint,
type SolutionBlueprint,
} from './solution-blueprint.zod';
const validBlueprint: SolutionBlueprint = {
summary: 'A simple project tracker',
assumptions: ['Projects own many tasks', 'Tasks have a status'],
objects: [
{
name: 'project',
label: 'Project',
fields: [
{ name: 'name', label: 'Name', type: 'text', required: true },
{ name: 'due_date', type: 'date' },
],
},
{
name: 'task',
label: 'Task',
fields: [
{ name: 'title', type: 'text', required: true },
{ name: 'status', type: 'select', options: [{ label: 'Open', value: 'open' }, { label: 'Done', value: 'done' }] },
{ name: 'project_id', type: 'lookup', reference: 'project' },
],
},
],
views: [
{ object: 'task', name: 'open_tasks', label: 'Open Tasks', type: 'list', columns: ['title', 'status'] },
],
};
describe('SolutionBlueprintSchema', () => {
it('parses a valid blueprint', () => {
const parsed = SolutionBlueprintSchema.parse(validBlueprint);
expect(parsed.objects).toHaveLength(2);
expect(parsed.objects[1].fields[2]).toMatchObject({ type: 'lookup', reference: 'project' });
expect(parsed.views?.[0].type).toBe('list');
});
it('defaults assumptions to an empty array and view type to list', () => {
const parsed = SolutionBlueprintSchema.parse({
summary: 'minimal',
objects: [{ name: 'thing', fields: [{ name: 'name', type: 'text' }] }],
views: [{ object: 'thing', name: 'all_things', columns: ['name'] }],
});
expect(parsed.assumptions).toEqual([]);
expect(parsed.views?.[0].type).toBe('list');
});
it('rejects a missing summary', () => {
const { summary: _drop, ...noSummary } = validBlueprint;
expect(() => SolutionBlueprintSchema.parse(noSummary)).toThrow();
});
it('rejects an invalid field type', () => {
expect(() =>
SolutionBlueprintSchema.parse({
summary: 'bad',
objects: [{ name: 'x', fields: [{ name: 'f', type: 'not_a_real_type' }] }],
}),
).toThrow();
});
it('rejects a non-snake_case object name', () => {
expect(() =>
SolutionBlueprintSchema.parse({
summary: 'bad',
objects: [{ name: 'MyObject', fields: [{ name: 'f', type: 'text' }] }],
}),
).toThrow();
});
it('rejects more than 2 clarifying questions', () => {
expect(() =>
SolutionBlueprintSchema.parse({
summary: 'too many questions',
objects: [{ name: 'x', fields: [{ name: 'f', type: 'text' }] }],
questions: ['a?', 'b?', 'c?'],
}),
).toThrow();
});
it('defineSolutionBlueprint validates and returns the parsed value', () => {
const bp = defineSolutionBlueprint(validBlueprint);
expect(bp.summary).toBe('A simple project tracker');
});
it('accepts an optional app with explicit nav', () => {
const parsed = SolutionBlueprintSchema.parse({
...validBlueprint,
app: {
name: 'project_mgmt',
label: 'Project Management',
icon: 'kanban',
nav: [
{ type: 'object', target: 'project', label: 'Projects' },
{ type: 'object', target: 'task' },
{ type: 'dashboard', target: 'overview' },
],
},
});
expect(parsed.app?.name).toBe('project_mgmt');
expect(parsed.app?.nav).toHaveLength(3);
expect(parsed.app?.nav?.[1].type).toBe('object'); // default applied
});
it('allows an app with no nav (auto-surfaced at apply time)', () => {
const parsed = SolutionBlueprintSchema.parse({
...validBlueprint,
app: { name: 'pm', label: 'PM' },
});
expect(parsed.app?.nav).toBeUndefined();
});
it('app is optional', () => {
expect(SolutionBlueprintSchema.parse(validBlueprint).app).toBeUndefined();
});
it('rejects a non-snake_case app name', () => {
expect(() =>
SolutionBlueprintSchema.parse({ ...validBlueprint, app: { name: 'MyApp' } }),
).toThrow();
});
it('rejects an invalid nav item type', () => {
expect(() =>
SolutionBlueprintSchema.parse({
...validBlueprint,
app: { name: 'pm', nav: [{ type: 'flow', target: 'project' }] },
}),
).toThrow();
});
});
// The strict mirror is what `generateObject` sends to OpenAI: every property
// must be present in `required` (optional → nullable), and no open `z.record`
// (seedData dropped). A live run proved the lenient schema's optional fields
// made OpenAI strict structured outputs reject the request.
describe('SolutionBlueprintStrictSchema (OpenAI strict mirror)', () => {
const strictBp = {
summary: 's',
assumptions: [],
questions: null,
objects: [
{
name: 'project',
label: null,
description: null,
fields: [
{ name: 'name', label: null, type: 'text', required: null, reference: null, options: null },
],
},
],
views: null,
dashboards: null,
app: null,
};
it('accepts a blueprint with null for every optional field', () => {
const parsed = SolutionBlueprintStrictSchema.parse(strictBp);
expect(parsed.objects[0].fields[0].type).toBe('text');
expect(parsed.views).toBeNull();
expect(parsed.app).toBeNull();
});
it('requires every top-level key to be present (OpenAI strict needs all in `required`)', () => {
const { views: _v, ...missingViews } = strictBp;
expect(() => SolutionBlueprintStrictSchema.parse(missingViews)).toThrow();
});
it('requires every (nullable) field key to be present — omitting `label` throws', () => {
const badField = {
...strictBp,
objects: [
{ name: 'x', label: null, description: null, fields: [{ name: 'f', type: 'text', required: null, reference: null, options: null }] },
],
};
// `f` is missing the (nullable, required) `label` key.
expect(() => SolutionBlueprintStrictSchema.parse(badField)).toThrow();
});
it('drops the un-strict-able seedData record (OpenAI strict cannot represent open key/value maps)', () => {
expect('seedData' in SolutionBlueprintStrictSchema.shape).toBe(false);
});
});