-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvisualize-data.tool.test.ts
More file actions
175 lines (155 loc) · 5.77 KB
/
Copy pathvisualize-data.tool.test.ts
File metadata and controls
175 lines (155 loc) · 5.77 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect } from 'vitest';
import type { AnalyticsQuery, AnalyticsResult } from '@objectstack/spec/contracts';
import { createVisualizeDataHandler, type VisualizeDataToolContext } from './visualize-data.tool.js';
import type { ToolExecutionContext } from './tool-registry.js';
/**
* `visualize_data` runs an analytics aggregation and emits the chart-ready
* result as a `data-chart` custom stream part (via `ctx.onProgress`), while
* returning a compact textual summary for the model to narrate. These tests
* pin both halves of that contract.
*/
/** Build a tool context whose analytics service records the query it received. */
function makeCtx(opts: {
result?: AnalyticsResult;
onQuery?: (q: AnalyticsQuery) => void;
throwError?: string;
}): VisualizeDataToolContext {
return {
analytics: {
query: async (q: AnalyticsQuery): Promise<AnalyticsResult> => {
opts.onQuery?.(q);
if (opts.throwError) throw new Error(opts.throwError);
return (
opts.result ?? {
rows: [
{ status: 'won', count: 5 },
{ status: 'lost', count: 2 },
],
fields: [
{ name: 'status', type: 'string' },
{ name: 'count', type: 'number', label: 'Count' },
],
}
);
},
getMeta: async () => [],
} as never,
};
}
/** Collect the `data-chart` parts emitted through onProgress. */
function makeExecCtx(): { ctx: ToolExecutionContext; parts: Array<{ type: string; id?: string; data?: unknown }> } {
const parts: Array<{ type: string; id?: string; data?: unknown }> = [];
const ctx: ToolExecutionContext = {
onProgress: (p) => parts.push(p),
};
return { ctx, parts };
}
describe('visualize_data', () => {
it('emits a data-chart part shaped for the SDUI <chart> renderer and returns a summary', async () => {
const handler = createVisualizeDataHandler(makeCtx({}));
const { ctx, parts } = makeExecCtx();
const out = JSON.parse(
(await handler(
{
objectName: 'opportunity',
dimension: 'status',
measures: [{ function: 'count' }],
chartType: 'bar',
title: 'Deals by status',
},
ctx,
)) as string,
);
// One data-chart part emitted, carrying the chart descriptor.
expect(parts).toHaveLength(1);
expect(parts[0].type).toBe('data-chart');
expect(parts[0].id).toBeTruthy();
const chart = parts[0].data as Record<string, any>;
expect(chart.type).toBe('chart');
expect(chart.chartType).toBe('bar');
expect(chart.title).toBe('Deals by status');
expect(chart.xAxisKey).toBe('status');
expect(chart.series).toEqual([{ dataKey: 'count', label: 'Count' }]);
expect(chart.data).toHaveLength(2);
// Textual summary for the model — names the chart, not a raw table dump.
expect(out.rendered).toBe('chart');
expect(out.categories).toBe(2);
expect(out.measures).toEqual(['count']);
});
it('maps function+field to the analytics suffix measure key (amount_sum)', async () => {
let received: AnalyticsQuery | undefined;
const handler = createVisualizeDataHandler(
makeCtx({
onQuery: (q) => (received = q),
result: {
rows: [{ region: 'NA', amount_sum: 1000 }],
fields: [
{ name: 'region', type: 'string' },
{ name: 'amount_sum', type: 'number' },
],
},
}),
);
const { ctx, parts } = makeExecCtx();
await handler(
{
objectName: 'order',
dimension: 'region',
measures: [{ function: 'sum', field: 'amount', label: 'Revenue' }],
where: { stage: { $ne: 'draft' } },
},
ctx,
);
expect(received?.cube).toBe('order');
expect(received?.measures).toEqual(['amount_sum']);
expect(received?.dimensions).toEqual(['region']);
expect(received?.where).toEqual({ stage: { $ne: 'draft' } });
// Series dataKey equals the measure key; the caller's label is preserved.
const chart = parts[0].data as Record<string, any>;
expect(chart.series).toEqual([{ dataKey: 'amount_sum', label: 'Revenue' }]);
});
it('defaults chartType to bar and supports multiple measures as series', async () => {
const handler = createVisualizeDataHandler(
makeCtx({
result: {
rows: [{ month: '2026-01', count: 10, amount_sum: 500 }],
fields: [],
},
}),
);
const { ctx, parts } = makeExecCtx();
await handler(
{
objectName: 'order',
dimension: 'month',
measures: [{ function: 'count' }, { function: 'sum', field: 'amount' }],
},
ctx,
);
const chart = parts[0].data as Record<string, any>;
expect(chart.chartType).toBe('bar');
expect(chart.series.map((s: any) => s.dataKey)).toEqual(['count', 'amount_sum']);
});
it('returns a structured error (no chart emitted) when the analytics query fails', async () => {
const handler = createVisualizeDataHandler(makeCtx({ throwError: 'no such cube' }));
const { ctx, parts } = makeExecCtx();
const out = JSON.parse(
(await handler(
{ objectName: 'ghost', dimension: 'x', measures: [{ function: 'count' }] },
ctx,
)) as string,
);
expect(parts).toHaveLength(0);
expect(out.error).toMatch(/Analytics query failed: no such cube/);
});
it('rejects calls with no valid measures', async () => {
const handler = createVisualizeDataHandler(makeCtx({}));
const { ctx, parts } = makeExecCtx();
const out = JSON.parse(
(await handler({ objectName: 'order', measures: [] }, ctx)) as string,
);
expect(parts).toHaveLength(0);
expect(out.error).toMatch(/at least one measure/i);
});
});