Skip to content

Commit f4129b9

Browse files
committed
add tests
1 parent 000c4bd commit f4129b9

File tree

4 files changed

+849
-0
lines changed

4 files changed

+849
-0
lines changed

test/actor_schema.test.ts

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import { getActorSchemaValidator } from '@apify/json_schemas';
2+
3+
describe('actor.json', () => {
4+
const validator = getActorSchemaValidator();
5+
6+
describe('valid schemas', () => {
7+
it('should validate a minimal valid schema', () => {
8+
const schema = {
9+
actorSpecification: 1,
10+
name: 'my-actor',
11+
version: '1.0.0',
12+
};
13+
14+
const isValid = validator(schema);
15+
expect(isValid).toBe(true);
16+
});
17+
18+
it('should validate a complete valid schema', () => {
19+
const schema = {
20+
actorSpecification: 1,
21+
name: 'my-actor',
22+
title: 'My Actor',
23+
description: 'This is my actor',
24+
version: '1.0.0',
25+
buildTag: 'latest',
26+
environmentVariables: {
27+
API_KEY: 'my-api-key',
28+
},
29+
dockerfile: '../Dockerfile',
30+
readme: '../README.md',
31+
minMemoryMbytes: 256,
32+
maxMemoryMbytes: 1024,
33+
input: {
34+
type: 'object',
35+
properties: {
36+
url: {
37+
type: 'string',
38+
},
39+
},
40+
},
41+
inputSchema: {
42+
type: 'object',
43+
properties: {
44+
url: {
45+
type: 'string',
46+
},
47+
},
48+
},
49+
output: {
50+
actorOutputSchemaVersion: 1,
51+
properties: {
52+
result: {
53+
type: 'string',
54+
template: 'Result: {{result}}',
55+
},
56+
},
57+
},
58+
outputSchema: {
59+
actorOutputSchemaVersion: 1,
60+
properties: {
61+
result: {
62+
type: 'string',
63+
template: 'Result: {{result}}',
64+
},
65+
},
66+
},
67+
storages: {
68+
keyValueStore: {
69+
actorKeyValueStoreSchemaVersion: 1,
70+
title: 'My Key-Value Store',
71+
collections: {
72+
myCollection: {
73+
title: 'My Collection',
74+
keyPrefix: 'my-prefix-',
75+
contentTypes: ['application/json'],
76+
},
77+
},
78+
},
79+
dataset: {
80+
actorSpecification: 1,
81+
fields: {
82+
type: 'object',
83+
properties: {
84+
url: {
85+
type: 'string',
86+
},
87+
},
88+
},
89+
},
90+
requestQueue: 'my-request-queue',
91+
},
92+
usesStandbyMode: true,
93+
webServerSchema: {
94+
type: 'object',
95+
properties: {
96+
port: {
97+
type: 'integer',
98+
},
99+
},
100+
},
101+
webServerMcpPath: '/mcp',
102+
};
103+
104+
const isValid = validator(schema);
105+
expect(isValid).toBe(true);
106+
});
107+
108+
it('should validate with string references', () => {
109+
const schema = {
110+
actorSpecification: 1,
111+
name: 'my-actor',
112+
version: '1.0.0',
113+
input: 'input.json',
114+
inputSchema: 'input-schema.json',
115+
output: 'output.json',
116+
outputSchema: 'output-schema.json',
117+
storages: {
118+
keyValueStore: 'key-value-store.json',
119+
dataset: 'dataset.json',
120+
requestQueue: 'request-queue',
121+
},
122+
webServerSchema: 'web-server-schema.json',
123+
};
124+
125+
const isValid = validator(schema);
126+
expect(isValid).toBe(true);
127+
});
128+
});
129+
130+
describe('invalid schemas', () => {
131+
it('should not validate when missing required fields', () => {
132+
const schema = {
133+
actorSpecification: 1,
134+
name: 'my-actor',
135+
// missing version
136+
};
137+
138+
const isValid = validator(schema);
139+
expect(isValid).toBe(false);
140+
expect(validator.errors).toContainEqual(
141+
expect.objectContaining({
142+
keyword: 'required',
143+
params: expect.objectContaining({
144+
missingProperty: 'version',
145+
}),
146+
}),
147+
);
148+
});
149+
150+
it('should not validate with invalid actorSpecification', () => {
151+
const schema = {
152+
actorSpecification: 2, // invalid value
153+
name: 'my-actor',
154+
version: '1.0.0',
155+
};
156+
157+
const isValid = validator(schema);
158+
expect(isValid).toBe(false);
159+
expect(validator.errors).toContainEqual(
160+
expect.objectContaining({
161+
keyword: 'maximum',
162+
params: expect.objectContaining({
163+
comparison: '<=',
164+
limit: 1,
165+
}),
166+
}),
167+
);
168+
});
169+
170+
it('should not validate with invalid version format', () => {
171+
const schema = {
172+
actorSpecification: 1,
173+
name: 'my-actor',
174+
version: 'invalid-version', // invalid format
175+
};
176+
177+
const isValid = validator(schema);
178+
expect(isValid).toBe(false);
179+
expect(validator.errors).toContainEqual(
180+
expect.objectContaining({
181+
keyword: 'pattern',
182+
params: expect.objectContaining({
183+
pattern: '^([0-9]+)\\.([0-9]+)(\\.[0-9]+){0,1}$',
184+
}),
185+
}),
186+
);
187+
});
188+
189+
it('should not validate with invalid memory values', () => {
190+
const schema = {
191+
actorSpecification: 1,
192+
name: 'my-actor',
193+
version: '1.0.0',
194+
minMemoryMbytes: 64, // too low
195+
maxMemoryMbytes: 65536, // too high
196+
};
197+
198+
const isValid = validator(schema);
199+
expect(isValid).toBe(false);
200+
expect(validator.errors).toContainEqual(
201+
expect.objectContaining({
202+
keyword: 'minimum',
203+
params: expect.objectContaining({
204+
comparison: '>=',
205+
limit: 128,
206+
}),
207+
}),
208+
);
209+
});
210+
});
211+
});

0 commit comments

Comments
 (0)