-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathsync.test.ts
More file actions
121 lines (107 loc) · 3.69 KB
/
sync.test.ts
File metadata and controls
121 lines (107 loc) · 3.69 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
import { describe, it, expect, vi } from "vitest";
import { syncLlmCatalog } from "./sync.js";
import { defaultModelPrices } from "./defaultPrices.js";
const gpt4oDef = defaultModelPrices.find((m) => m.modelName === "gpt-4o");
if (!gpt4oDef) {
throw new Error("expected gpt-4o in defaultModelPrices");
}
describe("syncLlmCatalog", () => {
it("rebuilds pricing tiers and prices for existing default-source models", async () => {
const existingId = "existing-gpt4o";
const llmModelUpdate = vi.fn();
const llmPricingTierDeleteMany = vi.fn();
const llmPricingTierCreate = vi.fn();
const prisma = {
llmModel: {
findFirst: vi.fn(async (args: { where: { modelName: string } }) => {
if (args.where.modelName === "gpt-4o") {
return {
id: existingId,
source: "default",
provider: "openai",
description: "stale description",
contextWindow: 999,
maxOutputTokens: 888,
capabilities: ["legacy"],
isHidden: true,
baseModelName: "legacy-base",
};
}
return null;
}),
},
$transaction: vi.fn(async (fn: (tx: unknown) => Promise<void>) => {
await fn({
llmModel: { update: llmModelUpdate },
llmPricingTier: {
deleteMany: llmPricingTierDeleteMany,
create: llmPricingTierCreate,
},
});
}),
};
await syncLlmCatalog(prisma as never);
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
expect(llmModelUpdate).toHaveBeenCalledWith({
where: { id: existingId },
data: expect.objectContaining({
matchPattern: gpt4oDef.matchPattern,
startDate: gpt4oDef.startDate ? new Date(gpt4oDef.startDate) : null,
}),
});
expect(llmPricingTierDeleteMany).toHaveBeenCalledWith({
where: { modelId: existingId },
});
expect(llmPricingTierCreate).toHaveBeenCalledTimes(gpt4oDef.pricingTiers.length);
const firstTier = gpt4oDef.pricingTiers[0];
expect(llmPricingTierCreate).toHaveBeenCalledWith({
data: {
modelId: existingId,
name: firstTier.name,
isDefault: firstTier.isDefault,
priority: firstTier.priority,
conditions: firstTier.conditions,
prices: {
create: expect.arrayContaining(
Object.entries(firstTier.prices).map(([usageType, price]) => ({
modelId: existingId,
usageType,
price,
}))
),
},
},
});
const createCall = llmPricingTierCreate.mock.calls[0][0] as {
data: { prices: { create: { usageType: string; price: number; modelId: string }[] } };
};
expect(createCall.data.prices.create).toHaveLength(Object.keys(firstTier.prices).length);
});
it("does not rebuild pricing for non-default source models", async () => {
const prisma = {
llmModel: {
findFirst: vi.fn(async (args: { where: { modelName: string } }) => {
if (args.where.modelName === "gpt-4o") {
return {
id: "admin-edited",
source: "admin",
provider: null,
description: null,
contextWindow: null,
maxOutputTokens: null,
capabilities: [],
isHidden: false,
baseModelName: null,
};
}
return null;
}),
},
$transaction: vi.fn(),
};
const result = await syncLlmCatalog(prisma as never);
expect(prisma.$transaction).not.toHaveBeenCalled();
expect(result.modelsUpdated).toBe(0);
expect(result.modelsSkipped).toBeGreaterThan(0);
});
});