Skip to content

Commit bdb1d0c

Browse files
Copilothotlong
andcommitted
Add test files for shared schemas: enums, mapping, metadata-types, http, connector-auth
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent ae166fa commit bdb1d0c

5 files changed

Lines changed: 651 additions & 0 deletions

File tree

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import { describe, it, expect } from 'vitest';
2+
import {
3+
ConnectorOAuth2Schema,
4+
ConnectorAPIKeySchema,
5+
ConnectorBasicAuthSchema,
6+
ConnectorBearerAuthSchema,
7+
ConnectorNoAuthSchema,
8+
ConnectorAuthConfigSchema,
9+
} from './connector-auth.zod';
10+
11+
describe('ConnectorOAuth2Schema', () => {
12+
const validOAuth2 = {
13+
type: 'oauth2' as const,
14+
authorizationUrl: 'https://auth.example.com/authorize',
15+
tokenUrl: 'https://auth.example.com/token',
16+
clientId: 'my-client-id',
17+
clientSecret: 'my-client-secret',
18+
};
19+
20+
it('should accept valid minimal oauth2 config', () => {
21+
const result = ConnectorOAuth2Schema.parse(validOAuth2);
22+
expect(result.type).toBe('oauth2');
23+
expect(result.authorizationUrl).toBe('https://auth.example.com/authorize');
24+
});
25+
26+
it('should accept oauth2 with all optional fields', () => {
27+
const result = ConnectorOAuth2Schema.parse({
28+
...validOAuth2,
29+
scopes: ['read', 'write'],
30+
redirectUri: 'https://app.example.com/callback',
31+
refreshToken: 'refresh-token-value',
32+
tokenExpiry: 1700000000,
33+
});
34+
expect(result.scopes).toEqual(['read', 'write']);
35+
expect(result.redirectUri).toBe('https://app.example.com/callback');
36+
expect(result.refreshToken).toBe('refresh-token-value');
37+
expect(result.tokenExpiry).toBe(1700000000);
38+
});
39+
40+
it('should have optional fields as undefined when not provided', () => {
41+
const result = ConnectorOAuth2Schema.parse(validOAuth2);
42+
expect(result.scopes).toBeUndefined();
43+
expect(result.redirectUri).toBeUndefined();
44+
expect(result.refreshToken).toBeUndefined();
45+
expect(result.tokenExpiry).toBeUndefined();
46+
});
47+
48+
it('should reject invalid authorizationUrl', () => {
49+
expect(() =>
50+
ConnectorOAuth2Schema.parse({ ...validOAuth2, authorizationUrl: 'not-a-url' }),
51+
).toThrow();
52+
});
53+
54+
it('should reject invalid tokenUrl', () => {
55+
expect(() =>
56+
ConnectorOAuth2Schema.parse({ ...validOAuth2, tokenUrl: 'not-a-url' }),
57+
).toThrow();
58+
});
59+
60+
it('should reject missing required fields', () => {
61+
expect(() => ConnectorOAuth2Schema.parse({ type: 'oauth2' })).toThrow();
62+
expect(() =>
63+
ConnectorOAuth2Schema.parse({ type: 'oauth2', authorizationUrl: 'https://a.com' }),
64+
).toThrow();
65+
});
66+
});
67+
68+
describe('ConnectorAPIKeySchema', () => {
69+
it('should accept valid api-key config with default headerName', () => {
70+
const result = ConnectorAPIKeySchema.parse({
71+
type: 'api-key',
72+
key: 'my-api-key-123',
73+
});
74+
expect(result.type).toBe('api-key');
75+
expect(result.key).toBe('my-api-key-123');
76+
expect(result.headerName).toBe('X-API-Key');
77+
});
78+
79+
it('should accept custom headerName', () => {
80+
const result = ConnectorAPIKeySchema.parse({
81+
type: 'api-key',
82+
key: 'key123',
83+
headerName: 'Authorization',
84+
});
85+
expect(result.headerName).toBe('Authorization');
86+
});
87+
88+
it('should accept optional paramName', () => {
89+
const result = ConnectorAPIKeySchema.parse({
90+
type: 'api-key',
91+
key: 'key123',
92+
paramName: 'api_key',
93+
});
94+
expect(result.paramName).toBe('api_key');
95+
});
96+
97+
it('should reject missing key', () => {
98+
expect(() => ConnectorAPIKeySchema.parse({ type: 'api-key' })).toThrow();
99+
});
100+
});
101+
102+
describe('ConnectorBasicAuthSchema', () => {
103+
it('should accept valid basic auth', () => {
104+
const result = ConnectorBasicAuthSchema.parse({
105+
type: 'basic',
106+
username: 'admin',
107+
password: 'secret',
108+
});
109+
expect(result.type).toBe('basic');
110+
expect(result.username).toBe('admin');
111+
expect(result.password).toBe('secret');
112+
});
113+
114+
it('should reject missing username', () => {
115+
expect(() =>
116+
ConnectorBasicAuthSchema.parse({ type: 'basic', password: 'secret' }),
117+
).toThrow();
118+
});
119+
120+
it('should reject missing password', () => {
121+
expect(() =>
122+
ConnectorBasicAuthSchema.parse({ type: 'basic', username: 'admin' }),
123+
).toThrow();
124+
});
125+
});
126+
127+
describe('ConnectorBearerAuthSchema', () => {
128+
it('should accept valid bearer auth', () => {
129+
const result = ConnectorBearerAuthSchema.parse({
130+
type: 'bearer',
131+
token: 'my-bearer-token',
132+
});
133+
expect(result.type).toBe('bearer');
134+
expect(result.token).toBe('my-bearer-token');
135+
});
136+
137+
it('should reject missing token', () => {
138+
expect(() => ConnectorBearerAuthSchema.parse({ type: 'bearer' })).toThrow();
139+
});
140+
});
141+
142+
describe('ConnectorNoAuthSchema', () => {
143+
it('should accept valid no-auth config', () => {
144+
const result = ConnectorNoAuthSchema.parse({ type: 'none' });
145+
expect(result.type).toBe('none');
146+
});
147+
148+
it('should reject wrong type', () => {
149+
expect(() => ConnectorNoAuthSchema.parse({ type: 'other' })).toThrow();
150+
});
151+
});
152+
153+
describe('ConnectorAuthConfigSchema', () => {
154+
it('should accept oauth2 via discriminated union', () => {
155+
const result = ConnectorAuthConfigSchema.parse({
156+
type: 'oauth2',
157+
authorizationUrl: 'https://auth.example.com/authorize',
158+
tokenUrl: 'https://auth.example.com/token',
159+
clientId: 'id',
160+
clientSecret: 'secret',
161+
});
162+
expect(result.type).toBe('oauth2');
163+
});
164+
165+
it('should accept api-key via discriminated union', () => {
166+
const result = ConnectorAuthConfigSchema.parse({
167+
type: 'api-key',
168+
key: 'key123',
169+
});
170+
expect(result.type).toBe('api-key');
171+
});
172+
173+
it('should accept basic via discriminated union', () => {
174+
const result = ConnectorAuthConfigSchema.parse({
175+
type: 'basic',
176+
username: 'user',
177+
password: 'pass',
178+
});
179+
expect(result.type).toBe('basic');
180+
});
181+
182+
it('should accept bearer via discriminated union', () => {
183+
const result = ConnectorAuthConfigSchema.parse({
184+
type: 'bearer',
185+
token: 'tok',
186+
});
187+
expect(result.type).toBe('bearer');
188+
});
189+
190+
it('should accept none via discriminated union', () => {
191+
const result = ConnectorAuthConfigSchema.parse({ type: 'none' });
192+
expect(result.type).toBe('none');
193+
});
194+
195+
it('should reject unknown auth type', () => {
196+
expect(() => ConnectorAuthConfigSchema.parse({ type: 'custom' })).toThrow();
197+
});
198+
199+
it('should reject missing type', () => {
200+
expect(() => ConnectorAuthConfigSchema.parse({ key: 'value' })).toThrow();
201+
});
202+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { describe, it, expect } from 'vitest';
2+
import {
3+
AggregationFunctionEnum,
4+
SortDirectionEnum,
5+
MutationEventEnum,
6+
IsolationLevelEnum,
7+
CacheStrategyEnum,
8+
} from './enums.zod';
9+
10+
describe('AggregationFunctionEnum', () => {
11+
it('should accept all valid aggregation functions', () => {
12+
const valid = [
13+
'count', 'sum', 'avg', 'min', 'max',
14+
'count_distinct', 'percentile', 'median', 'stddev', 'variance',
15+
];
16+
valid.forEach((v) => {
17+
expect(() => AggregationFunctionEnum.parse(v)).not.toThrow();
18+
});
19+
});
20+
21+
it('should reject invalid values', () => {
22+
const invalid = ['COUNT', 'SUM', 'average', 'total', '', 'unknown'];
23+
invalid.forEach((v) => {
24+
expect(() => AggregationFunctionEnum.parse(v)).toThrow();
25+
});
26+
});
27+
28+
it('should reject non-string types', () => {
29+
expect(() => AggregationFunctionEnum.parse(123)).toThrow();
30+
expect(() => AggregationFunctionEnum.parse(null)).toThrow();
31+
expect(() => AggregationFunctionEnum.parse(undefined)).toThrow();
32+
});
33+
});
34+
35+
describe('SortDirectionEnum', () => {
36+
it('should accept asc and desc', () => {
37+
expect(SortDirectionEnum.parse('asc')).toBe('asc');
38+
expect(SortDirectionEnum.parse('desc')).toBe('desc');
39+
});
40+
41+
it('should reject invalid values', () => {
42+
expect(() => SortDirectionEnum.parse('ASC')).toThrow();
43+
expect(() => SortDirectionEnum.parse('DESC')).toThrow();
44+
expect(() => SortDirectionEnum.parse('ascending')).toThrow();
45+
expect(() => SortDirectionEnum.parse('')).toThrow();
46+
});
47+
});
48+
49+
describe('MutationEventEnum', () => {
50+
it('should accept all mutation events', () => {
51+
const valid = ['insert', 'update', 'delete', 'upsert'];
52+
valid.forEach((v) => {
53+
expect(MutationEventEnum.parse(v)).toBe(v);
54+
});
55+
});
56+
57+
it('should reject invalid values', () => {
58+
expect(() => MutationEventEnum.parse('INSERT')).toThrow();
59+
expect(() => MutationEventEnum.parse('create')).toThrow();
60+
expect(() => MutationEventEnum.parse('remove')).toThrow();
61+
});
62+
});
63+
64+
describe('IsolationLevelEnum', () => {
65+
it('should accept all isolation levels', () => {
66+
const valid = [
67+
'read_uncommitted', 'read_committed', 'repeatable_read', 'serializable', 'snapshot',
68+
];
69+
valid.forEach((v) => {
70+
expect(IsolationLevelEnum.parse(v)).toBe(v);
71+
});
72+
});
73+
74+
it('should reject invalid values', () => {
75+
expect(() => IsolationLevelEnum.parse('READ_COMMITTED')).toThrow();
76+
expect(() => IsolationLevelEnum.parse('read-committed')).toThrow();
77+
expect(() => IsolationLevelEnum.parse('none')).toThrow();
78+
});
79+
});
80+
81+
describe('CacheStrategyEnum', () => {
82+
it('should accept all cache strategies', () => {
83+
const valid = ['lru', 'lfu', 'ttl', 'fifo'];
84+
valid.forEach((v) => {
85+
expect(CacheStrategyEnum.parse(v)).toBe(v);
86+
});
87+
});
88+
89+
it('should reject invalid values', () => {
90+
expect(() => CacheStrategyEnum.parse('LRU')).toThrow();
91+
expect(() => CacheStrategyEnum.parse('random')).toThrow();
92+
expect(() => CacheStrategyEnum.parse('')).toThrow();
93+
});
94+
});

0 commit comments

Comments
 (0)