-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathindex.test.ts
197 lines (177 loc) · 5.9 KB
/
index.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
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
192
193
194
195
196
197
import * as fs from "fs";
import {
decodeSecretLocation,
FQSN,
FullSlug,
PlatformClient,
PlatformSecretStore,
Registry,
resolveFQSN,
resolveSecretLocationInProxy,
SecretLocation,
SecretResult,
SecretStore,
SecretType,
unrollAssistant,
} from "../index.js";
// Test e2e flows from raw yaml -> unroll -> client render -> resolve secrets on proxy
describe("E2E Scenarios", () => {
const userSecrets: Record<string, string> = {
OPENAI_API_KEY: "sk-123",
};
const orgSecrets: Record<string, string> = {
GEMINI_API_KEY: "gemini-api-key",
};
const proxyEnvSecrets: Record<string, string> = {
ANTHROPIC_API_KEY: "sk-ant-env",
GEMINI_API_KEY: "gemini-api-key-env",
};
const localUserSecretStore: SecretStore = {
get: async function (secretName: string): Promise<string | undefined> {
return userSecrets[secretName];
},
set: function (secretName: string, secretValue: string): Promise<void> {
throw new Error("Function not implemented.");
},
};
const platformClient: PlatformClient = {
resolveFQSNs: async function (
fqsns: FQSN[],
): Promise<(SecretResult | undefined)[]> {
return await Promise.all(
fqsns.map((fqsn) =>
resolveFQSN("test-user", fqsn, platformSecretStore, "test-org"),
),
);
},
};
const environmentSecretStore: SecretStore = {
get: async function (secretName: string): Promise<string | undefined> {
return proxyEnvSecrets[secretName];
},
set: function (secretName: string, secretValue: string): Promise<void> {
throw new Error("Function not implemented.");
},
};
const platformSecretStore: PlatformSecretStore = {
getSecretFromSecretLocation: async function (
secretLocation: SecretLocation,
): Promise<string | undefined> {
switch (secretLocation.secretType) {
case SecretType.Package:
return undefined;
case SecretType.User:
return userSecrets[secretLocation.secretName];
case SecretType.Organization:
return orgSecrets[secretLocation.secretName];
case SecretType.ModelsAddOn:
case SecretType.FreeTrial:
if (
secretLocation.blockSlug.ownerSlug === "test-org" &&
secretLocation.blockSlug.packageSlug === "claude35sonnet" &&
secretLocation.secretName === "ANTHROPIC_API_KEY"
) {
return "sk-ant";
}
return undefined;
case SecretType.NotFound:
return undefined;
default:
return undefined;
}
},
};
const registry: Registry = {
getContent: async function (fullSlug: FullSlug): Promise<string> {
return fs
.readFileSync(
`./test/packages/${fullSlug.ownerSlug}/${fullSlug.packageSlug}.yaml`,
)
.toString();
},
};
it.only("should unroll assistant with a single block that doesn't exist", async () => {
const unrolledConfig = await unrollAssistant(
"test-org/assistant-with-non-existing-block",
registry,
{
renderSecrets: true,
platformClient,
orgScopeId: "test-org",
currentUserSlug: "test-user",
onPremProxyUrl: null,
},
);
expect(unrolledConfig.rules?.[0]).toBeNull();
});
it("should correctly unroll assistant", async () => {
const unrolledConfig = await unrollAssistant(
"test-org/assistant",
registry,
{
renderSecrets: true,
platformClient,
orgScopeId: "test-org",
currentUserSlug: "test-user",
onPremProxyUrl: null,
},
);
// Test that packages were correctly unrolled and params replaced
expect(unrolledConfig.models?.length).toBe(3);
const openAiModel = unrolledConfig.models?.[0]!;
expect(openAiModel.apiKey).toBe("sk-123");
const geminiModel = unrolledConfig.models?.[1]!;
expect(geminiModel.provider).toBe("continue-proxy");
expect(geminiModel.apiKey).toBeUndefined();
const geminiSecretLocation = "organization:test-org/GEMINI_API_KEY";
expect((geminiModel as any).apiKeyLocation).toBe(geminiSecretLocation);
const anthropicModel = unrolledConfig.models?.[2]!;
expect(anthropicModel.provider).toBe("continue-proxy");
expect(anthropicModel.apiKey).toBeUndefined();
const anthropicSecretLocation =
"models_add_on:test-org/claude35sonnet/ANTHROPIC_API_KEY";
expect((anthropicModel as any).apiKeyLocation).toBe(
anthropicSecretLocation,
);
expect(unrolledConfig.rules?.length).toBe(2);
expect(unrolledConfig.docs?.[0]?.startUrl).toBe(
"https://docs.python.org/release/3.13.1",
);
expect(unrolledConfig.docs?.[0]?.rootUrl).toBe(
"https://docs.python.org/release/3.13.1",
);
// Test that proxy can correctly resolve secrets
const decodedAnthropicSecretLocation = decodeSecretLocation(
anthropicSecretLocation,
);
const decodedGeminiSecretLocation =
decodeSecretLocation(geminiSecretLocation);
// With environment
const antSecretValue = await resolveSecretLocationInProxy(
decodedAnthropicSecretLocation,
platformSecretStore,
environmentSecretStore,
);
expect(antSecretValue).toBe("sk-ant-env");
const geminiSecretValue = await resolveSecretLocationInProxy(
decodedGeminiSecretLocation,
platformSecretStore,
environmentSecretStore,
);
expect(geminiSecretValue).toBe("gemini-api-key-env");
// Without environment
const antSecretValue2 = await resolveSecretLocationInProxy(
decodedAnthropicSecretLocation,
platformSecretStore,
undefined,
);
expect(antSecretValue2).toBe("sk-ant");
const geminiSecretValue2 = await resolveSecretLocationInProxy(
decodedGeminiSecretLocation,
platformSecretStore,
undefined,
);
expect(geminiSecretValue2).toBe("gemini-api-key");
});
it.skip("should prioritize org over user / package secrets", () => {});
});