forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_extraction_v0.test.ts
129 lines (122 loc) · 4.05 KB
/
cluster_extraction_v0.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
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
import { describe, it, vi } from 'vitest';
import { expect } from 'vitest';
import ClusterExtractionNode, {
cluster_extraction_node_data_v0
} from '$lib/compute/cluster_extraction_v0';
import deepCopy from 'deep-copy';
import mock_cluster_extraction_data from '$lib/mock_data/cluster_extraction/cluster_extraction.json';
import csv_data from '$lib/mock_data/csv/csv.json';
import prompt from '$lib/mock_data/cluster_extraction/prompt.txt?raw';
import system_prompt from '$lib/mock_data/cluster_extraction/system_prompt.txt?raw';
import { format, unwrapFunctionStore } from 'svelte-i18n';
const $__ = unwrapFunctionStore(format);
vi.mock('$lib/utils', () => ({
readFileFromGCS: vi.fn(() => Promise.resolve(JSON.stringify(mock_cluster_extraction_data))),
uploadJSONToGCS: vi.fn(() => Promise.resolve())
}));
describe('ClusterExtractionNode class', () => {
it('extract the cluster', async () => {
const node = new ClusterExtractionNode(deepCopy(cluster_extraction_node_data_v0));
node.data.prompt = prompt;
node.data.system_prompt = system_prompt;
const inputData = {
open_ai_key: 'sk-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
csv: csv_data
};
const output = await node.compute(
inputData,
'run',
console.log,
console.error,
console.log,
'test_slug',
null
);
expect(output).toEqual(mock_cluster_extraction_data);
expect(node.data.message).toEqual(`topics: 2 subtopics: 2.`);
});
it('should not extract the cluster if no csv', async () => {
const node = new ClusterExtractionNode(deepCopy(cluster_extraction_node_data_v0));
node.data.prompt = prompt;
node.data.system_prompt = system_prompt;
const inputData = {
open_ai_key: 'sk-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
};
const output = await node.compute(
inputData,
'run',
console.log,
console.error,
console.log,
'test_slug',
null
);
expect(output).toEqual(undefined);
expect(node.data.message).toEqual(`missing_input_data`);
});
it('should not extract the cluster if no open_ai_key', async () => {
const node = new ClusterExtractionNode(deepCopy(cluster_extraction_node_data_v0));
node.data.prompt = prompt;
node.data.system_prompt = system_prompt;
const inputData = {
csv: csv_data
};
const output = await node.compute(
inputData,
'run',
console.log,
console.error,
console.log,
'test_slug',
null
);
expect(output).toEqual(undefined);
expect(node.data.message).toEqual(`missing_input_data`);
});
it('should not extract the cluster if no prompt and no system prompt', async () => {
const node = new ClusterExtractionNode(deepCopy(cluster_extraction_node_data_v0));
node.data.system_prompt = undefined;
node.data.prompt = undefined;
const inputData = {
open_ai_key: 'sk-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
csv: csv_data
};
const output = await node.compute(
inputData,
'run',
console.log,
console.error,
console.log,
'test_slug',
null
);
expect(output).toEqual(undefined);
expect(node.data.message).toEqual(`missing_input_data`);
});
it('test GCS caching', async () => {
const node = new ClusterExtractionNode(deepCopy(cluster_extraction_node_data_v0));
node.data.prompt = prompt;
node.data.system_prompt = system_prompt;
node.data.dirty = false;
node.data.csv_length = csv_data.length;
node.data.gcs_path = 'gs://test_bucket/test_path';
const inputData = {
open_ai_key: 'sk-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
csv: csv_data
};
const output = await node.compute(
inputData,
'run',
(x) => {
console.log(x);
expect(x == 'Calling OpenAI').toEqual(false);
},
console.error,
console.log,
'test_slug',
null
);
expect(output).toEqual(mock_cluster_extraction_data);
expect(node.data.message).toEqual(`loaded_from_gcs. topics: 2 subtopics: 2.`);
});
});