Open History to see the last commit
1import { env } from 'cloudflare:test'
2import { and, eq, isNull } from 'drizzle-orm'
3import { getDb } from '#/db/index.ts'
4import { endpoints, providers, schemaVersions } from '#/db/schema.ts'
5import { syncProvider } from '../ingest/sync.ts'
6import type { ProviderConfig } from './types.ts'
7import { expect, it } from 'vitest'
8import {
9 DEEPSEEK_CHAT_DOCS,
10 decodeDeepseekOperation,
11 nativeDeepseekDocument,
12} from './deepseek-native-spec.ts'
13
14it('decodes native deflate operation payloads in workerd without eval', async () => {
15 const operation = await decodeDeepseekOperation(
16 'api:"eJyljrEOwjAMRH8FeUYEMTLyG4ghpCcSaGMTG6Sq4t9JAgszns7npzsvpChPFKX96rjQo4xVUDQT3TvnJW0GQBS4bQJP9DqtVzTBIg+NE1aj6oi32HYXojdXQRlhibO2Y8H9AbUDD3NlFgqcDdm69iJjCr6x7qqcu6khYvJd2ixowXy+InyqCguKJWgHJh4w/qBqJeULvfr0epX6yZffbbf/PvFJfgM4KWlp"',
17 )
18 expect(
19 nativeDeepseekDocument(operation).paths?.['/chat/completions']?.post
20 ?.requestBody,
21 ).toEqual(operation.requestBody)
22})
23
24it('replaces both current borrowed schema kinds with native fragments', async () => {
25 const db = getDb(env)
26 const id = 'deepseek-native-replacement'
27 const endpointId = `${id}/chat/completions`
28 await db.insert(providers).values({
29 id,
30 displayName: 'Native DeepSeek test',
31 specSourceUrl: DEEPSEEK_CHAT_DOCS,
32 })
33 await db.insert(endpoints).values({
34 id: endpointId,
35 providerId: id,
36 activity: 'chat',
37 method: 'POST',
38 path: '/chat/completions',
39 })
40 for (const kind of ['input', 'output'] as const) {
41 await db.insert(schemaVersions).values({
42 id: `${endpointId}:${kind}:borrowed`,
43 endpointId,
44 kind,
45 contentHash: 'b'.repeat(64),
46 schema: JSON.stringify({
47 type: 'object',
48 properties: { borrowed: { type: 'string' } },
49 }),
50 sourceUrl:
51 'https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml',
52 sourceHash: 'c'.repeat(64),
53 derivation: 'generated',
54 createdAt: 1,
55 })
56 }
57 const native = nativeDeepseekDocument({
58 servers: [{ url: 'https://api.deepseek.com' }],
59 method: 'post',
60 path: '/chat/completions',
61 requestBody: {
62 content: {
63 'application/json': {
64 schema: {
65 type: 'object',
66 properties: { messages: { type: 'array' } },
67 },
68 },
69 },
70 },
71 responses: {
72 '200 (No streaming)': {
73 content: {
74 'application/json': {
75 schema: {
76 type: 'object',
77 properties: { choices: { type: 'array' } },
78 },
79 },
80 },
81 },
82 },
83 })
84 const provider: ProviderConfig = {
85 id,
86 displayName: 'Native DeepSeek test',
87 defaultDerivation: 'generated',
88 fetchSpec: async () => ({
89 specs: [native],
90 sources: [{ url: DEEPSEEK_CHAT_DOCS, hash: 'd'.repeat(64) }],
91 outputStrategy: 'post-200',
92 }),
93 listModels: async () => ({ models: [] }),
94 classify: (path) => (path === '/chat/completions' ? 'chat' : null),
95 }
96 const result = await syncProvider(
97 { db, kv: env.SCHEMA_CACHE, secrets: {}, now: () => 10 },
98 provider,
99 )
100 expect(result.versionsAdded).toBe(2)
101 const current = await db
102 .select()
103 .from(schemaVersions)
104 .where(
105 and(
106 eq(schemaVersions.endpointId, endpointId),
107 isNull(schemaVersions.supersededAt),
108 ),
109 )
110 expect(current).toHaveLength(2)
111 expect(current.every((row) => row.sourceUrl === DEEPSEEK_CHAT_DOCS)).toBe(
112 true,
113 )
114 expect(current.every((row) => !row.schema.includes('borrowed'))).toBe(true)
115})