-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathapply-extension-spec.test.ts
More file actions
142 lines (127 loc) · 4.59 KB
/
apply-extension-spec.test.ts
File metadata and controls
142 lines (127 loc) · 4.59 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import applyExtensionSpec from "./apply-extension-spec";
import { OpenrpcDocument } from "./types";
import goodSchema from "./extension-good-schema.json";
import getExtendedMetaSchema from "./get-extended-metaschema";
describe("applyExtensionSpec", () => {
it("should successfully apply extension spec to meta schema", () => {
const result = applyExtensionSpec(goodSchema as OpenrpcDocument, getExtendedMetaSchema("1.3"));
// Check if extension was applied to methodObject
const methodObjectDef = result.definitions.methodObject;
expect(methodObjectDef.properties["x-notification"]).toBeDefined();
expect(methodObjectDef.properties["x-notification"].type).toBe("boolean");
expect(methodObjectDef.properties["x-notification"].description).toBe(
"Whether or not this method is a notification or not"
);
expect(methodObjectDef.properties["x-notification"].summary).toBe("OpenRPC Notification");
});
it("should handle multiple extensions", () => {
const multiExtensionDoc = {
...goodSchema,
"x-extensions": [
...goodSchema["x-extensions"],
{
...goodSchema["x-extensions"][0],
name: "x-another-extension",
schema: { type: "string" },
},
],
};
const result = applyExtensionSpec(
multiExtensionDoc as OpenrpcDocument,
getExtendedMetaSchema()
);
const methodObjectDef = result.definitions.methodObject;
expect(methodObjectDef.properties["x-notification"]).toBeDefined();
expect(methodObjectDef.properties["x-another-extension"]).toBeDefined();
expect(methodObjectDef.properties["x-another-extension"].type).toBe("string");
});
it("should return unmodified schema when x-extensions is empty", () => {
const emptyExtensionsDoc = {
...goodSchema,
"x-extensions": [],
};
const schema = getExtendedMetaSchema("1.3");
const result = applyExtensionSpec(emptyExtensionsDoc as OpenrpcDocument, schema);
expect(result).toEqual(schema);
});
it("should throw error when restricted schema definition doesn't exist", () => {
const badDoc = {
...goodSchema,
"x-extensions": [
{
...goodSchema["x-extensions"][0],
restricted: ["nonExistentDefinition"],
},
],
};
expect(() => {
applyExtensionSpec(badDoc as OpenrpcDocument, getExtendedMetaSchema("1.3"));
}).toThrow("nonExistentDefinition does not exist, cannot apply extension x-notification");
});
it("should allow restricted schema definition by extension name", () => {
const doc = {
...goodSchema,
"x-extensions": [
{
...goodSchema["x-extensions"][0],
name: "x-test-potate",
restricted: ["methodObject"],
schema: {
type: "object",
properties: {
id: { type: "string" },
title: { type: "string" },
description: { type: "string" },
},
},
},
{
...goodSchema["x-extensions"][0],
name: "x-next",
restricted: ["x-test-potate"], //search everything for key with x-error-groups
schema: {
type: "boolean",
},
},
],
};
const result = applyExtensionSpec(doc as OpenrpcDocument, getExtendedMetaSchema("1.3"));
expect(result.definitions["x-test-potate"]).toBeDefined();
expect(result.definitions["x-test-potate"].properties["x-next"]).toBeDefined();
expect(result.definitions["x-test-potate"].properties["x-next"].type).toBe("boolean");
});
it("should throw error when extension name already exists in definitions", () => {
const doc = {
...goodSchema,
"x-extensions": [
{
...goodSchema["x-extensions"][0],
name: "methodObject",
},
],
};
expect(() => {
applyExtensionSpec(doc as OpenrpcDocument, getExtendedMetaSchema());
}).toThrow("methodObject already exists in definitions, cannot apply extension methodObject");
});
it("should throw error when extension property already exists", () => {
const schema = getExtendedMetaSchema("1.3");
const modifiedSchema = {
...schema,
definitions: {
...schema.definitions,
methodObject: {
...schema.definitions.methodObject,
properties: {
"x-notification": {}, // Already exists
},
},
},
};
expect(() => {
applyExtensionSpec(goodSchema as OpenrpcDocument, modifiedSchema);
}).toThrow(
"x-notification already exists in methodObject, cannot apply extension x-notification"
);
});
});