-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcjs.test.js
More file actions
71 lines (60 loc) · 2.45 KB
/
cjs.test.js
File metadata and controls
71 lines (60 loc) · 2.45 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
const test = require('node:test');
const assert = require('node:assert');
const getSchema = require('..');
test('loads a valid schema', () => {
const schema = getSchema('2020-12/misc/schema-like');
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
assert.strictEqual(schema.title, 'JSON Schema Document');
});
test('returns null for non-existent schema', () => {
const schema = getSchema('nonexistent/schema/path');
assert.strictEqual(schema, null);
});
test('returns null for invalid input types', () => {
assert.strictEqual(getSchema(null), null);
assert.strictEqual(getSchema(undefined), null);
assert.strictEqual(getSchema(123), null);
assert.strictEqual(getSchema({}), null);
});
test('blocks directory traversal attempts', () => {
const schema = getSchema('../package');
assert.strictEqual(schema, null);
});
test('blocks directory traversal with multiple levels', () => {
const schema = getSchema('../../etc/passwd');
assert.strictEqual(schema, null);
});
test('loads schema from nested path', () => {
const schema = getSchema('2020-12/w3c/xmlschema/2001/hex-binary');
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
});
test('lazy loads schema via schemas object', () => {
const { schemas } = require('..');
const schema = schemas['2020-12'].misc['schema-like'];
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.title, 'JSON Schema Document');
});
test('caches loaded schemas', () => {
const { schemas } = require('..');
const schema1 = schemas['2020-12'].misc['schema-like'];
const schema2 = schemas['2020-12'].misc['schema-like'];
assert.strictEqual(schema1, schema2);
});
test('handles deeply nested paths', () => {
const { schemas } = require('..');
const schema = schemas['2020-12'].w3c.xmlschema['2001']['hex-binary'];
assert.strictEqual(typeof schema, 'object');
assert.strictEqual(schema.$schema, 'https://json-schema.org/draft/2020-12/schema');
});
test('returns undefined for non-existent directories', () => {
const { schemas } = require('..');
const result = schemas['2020-12'].nonexistent;
assert.strictEqual(result, undefined);
});
test('returns undefined for non-existent files', () => {
const { schemas } = require('..');
const result = schemas['2020-12'].misc['nonexistent-file'];
assert.strictEqual(result, undefined);
});