@unispechq/unispec-core 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/converters/index.d.ts +13 -0
- package/dist/converters/index.js +89 -0
- package/dist/diff/index.d.ts +21 -0
- package/dist/diff/index.js +195 -0
- package/dist/index.js +6 -0
- package/dist/loader/index.d.ts +13 -0
- package/dist/loader/index.js +19 -0
- package/dist/normalizer/index.d.ts +11 -0
- package/dist/normalizer/index.js +116 -0
- package/dist/types/index.d.ts +57 -0
- package/dist/types/index.js +1 -0
- package/dist/validator/index.d.ts +7 -0
- package/dist/validator/index.js +47 -0
- package/package.json +5 -1
- package/.github/workflows/npm-publish.yml +0 -74
- package/.windsurfrules +0 -138
- package/scripts/release.js +0 -51
- package/src/converters/index.ts +0 -120
- package/src/diff/index.ts +0 -235
- package/src/loader/index.ts +0 -25
- package/src/normalizer/index.ts +0 -156
- package/src/types/index.ts +0 -67
- package/src/validator/index.ts +0 -61
- package/tests/converters.test.mjs +0 -126
- package/tests/diff.test.mjs +0 -240
- package/tests/loader-validator.test.mjs +0 -19
- package/tests/normalizer.test.mjs +0 -115
- package/tsconfig.json +0 -15
- /package/{src/index.ts → dist/index.d.ts} +0 -0
package/src/normalizer/index.ts
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { UniSpecDocument, UniSpecWebSocketProtocol, UniSpecWebSocketChannel, UniSpecWebSocketMessage } from "../types";
|
|
2
|
-
|
|
3
|
-
export interface NormalizeOptions {
|
|
4
|
-
// Placeholder for future normalization options
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
8
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function normalizeValue(value: unknown): unknown {
|
|
12
|
-
if (Array.isArray(value)) {
|
|
13
|
-
return value.map((item) => normalizeValue(item));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (isPlainObject(value)) {
|
|
17
|
-
const entries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b));
|
|
18
|
-
const normalized: Record<string, unknown> = {};
|
|
19
|
-
for (const [key, val] of entries) {
|
|
20
|
-
normalized[key] = normalizeValue(val);
|
|
21
|
-
}
|
|
22
|
-
return normalized;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return value;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function normalizeRestPaths(doc: UniSpecDocument): UniSpecDocument {
|
|
29
|
-
if (!doc || !doc.service || !doc.service.protocols) {
|
|
30
|
-
return doc;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const protocols: any = doc.service.protocols;
|
|
34
|
-
const rest: any = protocols.rest;
|
|
35
|
-
|
|
36
|
-
if (!rest || !rest.paths || typeof rest.paths !== "object") {
|
|
37
|
-
return doc;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const httpMethodOrder = ["get", "head", "options", "post", "put", "patch", "delete"];
|
|
41
|
-
|
|
42
|
-
const normalizedPaths: Record<string, unknown> = {};
|
|
43
|
-
|
|
44
|
-
for (const path of Object.keys(rest.paths).sort()) {
|
|
45
|
-
const pathItem = (rest.paths as any)[path];
|
|
46
|
-
|
|
47
|
-
if (!pathItem || typeof pathItem !== "object") {
|
|
48
|
-
normalizedPaths[path] = pathItem;
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const pathItemObj: Record<string, unknown> = pathItem as Record<string, unknown>;
|
|
53
|
-
|
|
54
|
-
const ordered: Record<string, unknown> = {};
|
|
55
|
-
|
|
56
|
-
for (const method of httpMethodOrder) {
|
|
57
|
-
if (Object.prototype.hasOwnProperty.call(pathItemObj, method)) {
|
|
58
|
-
ordered[method] = pathItemObj[method];
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const remainingKeys = Object.keys(pathItemObj).filter((k) => !httpMethodOrder.includes(k)).sort();
|
|
63
|
-
for (const key of remainingKeys) {
|
|
64
|
-
ordered[key] = pathItemObj[key];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
normalizedPaths[path] = ordered;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
rest.paths = normalizedPaths;
|
|
71
|
-
|
|
72
|
-
return doc;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function normalizeWebSocket(doc: UniSpecDocument): UniSpecDocument {
|
|
76
|
-
if (!doc || !doc.service || !doc.service.protocols) {
|
|
77
|
-
return doc;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const protocols: any = doc.service.protocols;
|
|
81
|
-
const websocket: UniSpecWebSocketProtocol | undefined = protocols.websocket;
|
|
82
|
-
|
|
83
|
-
if (!websocket || !websocket.channels || typeof websocket.channels !== "object") {
|
|
84
|
-
return doc;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const originalChannels = websocket.channels as Record<string, UniSpecWebSocketChannel>;
|
|
88
|
-
const sortedChannels: Record<string, UniSpecWebSocketChannel> = {};
|
|
89
|
-
|
|
90
|
-
for (const name of Object.keys(originalChannels).sort()) {
|
|
91
|
-
const channel = originalChannels[name];
|
|
92
|
-
if (!channel) {
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
let messages = channel.messages;
|
|
97
|
-
if (Array.isArray(messages)) {
|
|
98
|
-
messages = [...messages].sort((a: UniSpecWebSocketMessage, b: UniSpecWebSocketMessage) => {
|
|
99
|
-
const aName = a?.name ?? "";
|
|
100
|
-
const bName = b?.name ?? "";
|
|
101
|
-
return aName.localeCompare(bName);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
sortedChannels[name] = {
|
|
106
|
-
...channel,
|
|
107
|
-
...(messages ? { messages } : {}),
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
websocket.channels = sortedChannels;
|
|
112
|
-
|
|
113
|
-
return doc;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function normalizeGraphqlOperations(doc: UniSpecDocument): UniSpecDocument {
|
|
117
|
-
if (!doc || !doc.service || !doc.service.protocols) {
|
|
118
|
-
return doc;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const protocols: any = doc.service.protocols;
|
|
122
|
-
const graphql: any = protocols.graphql;
|
|
123
|
-
|
|
124
|
-
if (!graphql || !graphql.operations) {
|
|
125
|
-
return doc;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const kinds = ["queries", "mutations", "subscriptions"] as const;
|
|
129
|
-
|
|
130
|
-
for (const kind of kinds) {
|
|
131
|
-
const bucket = graphql.operations[kind];
|
|
132
|
-
if (!bucket || typeof bucket !== "object") {
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const sorted: Record<string, unknown> = {};
|
|
137
|
-
for (const name of Object.keys(bucket).sort()) {
|
|
138
|
-
sorted[name] = bucket[name];
|
|
139
|
-
}
|
|
140
|
-
graphql.operations[kind] = sorted;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return doc;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Normalize a UniSpec document into a canonical, deterministic form.
|
|
148
|
-
*
|
|
149
|
-
* Current behavior:
|
|
150
|
-
* - Recursively sorts object keys lexicographically.
|
|
151
|
-
* - Preserves values as-is.
|
|
152
|
-
*/
|
|
153
|
-
export function normalizeUniSpec(doc: UniSpecDocument, _options: NormalizeOptions = {}): UniSpecDocument {
|
|
154
|
-
const normalized = normalizeValue(doc) as UniSpecDocument;
|
|
155
|
-
return normalizeWebSocket(normalizeGraphqlOperations(normalizeRestPaths(normalized)));
|
|
156
|
-
}
|
package/src/types/index.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
export interface UniSpecGraphQLSchema {
|
|
2
|
-
sdl?: string;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export interface UniSpecGraphQLOperations {
|
|
6
|
-
queries?: Record<string, unknown>;
|
|
7
|
-
mutations?: Record<string, unknown>;
|
|
8
|
-
subscriptions?: Record<string, unknown>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface UniSpecGraphQLProtocol {
|
|
12
|
-
schema?: UniSpecGraphQLSchema;
|
|
13
|
-
operations?: UniSpecGraphQLOperations;
|
|
14
|
-
extensions?: Record<string, unknown>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface UniSpecWebSocketMessage {
|
|
18
|
-
name?: string;
|
|
19
|
-
summary?: string;
|
|
20
|
-
description?: string;
|
|
21
|
-
payload?: unknown;
|
|
22
|
-
extensions?: Record<string, unknown>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface UniSpecWebSocketChannel {
|
|
26
|
-
summary?: string;
|
|
27
|
-
title?: string;
|
|
28
|
-
description?: string;
|
|
29
|
-
direction?: string;
|
|
30
|
-
messages?: UniSpecWebSocketMessage[];
|
|
31
|
-
extensions?: Record<string, unknown>;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface UniSpecWebSocketProtocol {
|
|
35
|
-
channels?: Record<string, UniSpecWebSocketChannel>;
|
|
36
|
-
extensions?: Record<string, unknown>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface UniSpecServiceProtocols {
|
|
40
|
-
rest?: unknown;
|
|
41
|
-
graphql?: UniSpecGraphQLProtocol;
|
|
42
|
-
websocket?: UniSpecWebSocketProtocol;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface UniSpecService {
|
|
46
|
-
name: string;
|
|
47
|
-
title?: string;
|
|
48
|
-
description?: string;
|
|
49
|
-
protocols?: UniSpecServiceProtocols;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface UniSpecDocument {
|
|
53
|
-
unispecVersion: string;
|
|
54
|
-
service: UniSpecService;
|
|
55
|
-
extensions?: Record<string, unknown>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface ValidationError {
|
|
59
|
-
message: string;
|
|
60
|
-
path?: string;
|
|
61
|
-
code?: string;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export interface ValidationResult {
|
|
65
|
-
valid: boolean;
|
|
66
|
-
errors: ValidationError[];
|
|
67
|
-
}
|
package/src/validator/index.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import Ajv2020, { ErrorObject } from "ajv/dist/2020.js";
|
|
2
|
-
import { createRequire } from "module";
|
|
3
|
-
import { unispec as unispecSchema, manifest as unispecManifest } from "@unispechq/unispec-schema";
|
|
4
|
-
import { UniSpecDocument, ValidationResult } from "../types";
|
|
5
|
-
|
|
6
|
-
export interface ValidateOptions {
|
|
7
|
-
// Placeholder for future validation options (e.g., strict mode, schema version)
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const require = createRequire(import.meta.url);
|
|
11
|
-
|
|
12
|
-
const ajv = new Ajv2020({
|
|
13
|
-
allErrors: true,
|
|
14
|
-
strict: true,
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
// Register all UniSpec subschemas so that Ajv can resolve internal $ref links
|
|
18
|
-
try {
|
|
19
|
-
const schemaRootPath = require.resolve("@unispechq/unispec-schema");
|
|
20
|
-
const schemaDir = schemaRootPath.replace(/index\.(cjs|mjs|js)$/u, "schema/");
|
|
21
|
-
|
|
22
|
-
const types = (unispecManifest as any)?.types ?? {};
|
|
23
|
-
const typeSchemaPaths: string[] = Object.values(types).map((rel: unknown) => String(rel));
|
|
24
|
-
|
|
25
|
-
const loadedTypeSchemas = typeSchemaPaths.map((relPath) => require(schemaDir + relPath));
|
|
26
|
-
|
|
27
|
-
ajv.addSchema(loadedTypeSchemas as object | object[]);
|
|
28
|
-
} catch {
|
|
29
|
-
// If subschemas cannot be loaded for some reason, validation will still work for
|
|
30
|
-
// parts of the schema that do not rely on those $ref references.
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const validateFn = ajv.compile(unispecSchema as object);
|
|
34
|
-
|
|
35
|
-
function mapAjvErrors(errors: ErrorObject[] | null | undefined): ValidationResult["errors"] {
|
|
36
|
-
if (!errors) return [];
|
|
37
|
-
return errors.map((error) => ({
|
|
38
|
-
message: error.message || "UniSpec validation error",
|
|
39
|
-
path: error.instancePath || error.schemaPath,
|
|
40
|
-
code: error.keyword,
|
|
41
|
-
}));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Validate a UniSpec document against the UniSpec JSON Schema.
|
|
46
|
-
*/
|
|
47
|
-
export async function validateUniSpec(doc: UniSpecDocument, _options: ValidateOptions = {}): Promise<ValidationResult> {
|
|
48
|
-
const valid = validateFn(doc);
|
|
49
|
-
|
|
50
|
-
if (valid) {
|
|
51
|
-
return {
|
|
52
|
-
valid: true,
|
|
53
|
-
errors: [],
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
valid: false,
|
|
59
|
-
errors: mapAjvErrors(validateFn.errors),
|
|
60
|
-
};
|
|
61
|
-
}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
|
|
4
|
-
import * as core from "../dist/index.js";
|
|
5
|
-
|
|
6
|
-
const { toOpenAPI, toGraphQLSDL, toWebSocketModel } = core;
|
|
7
|
-
|
|
8
|
-
test("toOpenAPI produces basic OpenAPI structure from UniSpec document", () => {
|
|
9
|
-
const doc = {
|
|
10
|
-
unispecVersion: "0.1.0",
|
|
11
|
-
service: {
|
|
12
|
-
name: "test-service",
|
|
13
|
-
title: "Service",
|
|
14
|
-
description: "Desc",
|
|
15
|
-
protocols: {
|
|
16
|
-
rest: {
|
|
17
|
-
servers: [{ url: "https://api.example.com" }],
|
|
18
|
-
paths: {
|
|
19
|
-
"/ping": {
|
|
20
|
-
get: {},
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
components: {
|
|
24
|
-
schemas: {
|
|
25
|
-
PingResponse: {
|
|
26
|
-
type: "object",
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
security: [{ bearerAuth: [] }],
|
|
31
|
-
tags: [
|
|
32
|
-
{
|
|
33
|
-
name: "ping",
|
|
34
|
-
description: "Ping operations",
|
|
35
|
-
},
|
|
36
|
-
],
|
|
37
|
-
"x-extra-rest-field": true,
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const openapi = toOpenAPI(doc);
|
|
44
|
-
|
|
45
|
-
assert.equal(openapi.openapi, "3.1.0");
|
|
46
|
-
assert.equal(openapi.info.title, "Service");
|
|
47
|
-
assert.equal(openapi.info.description, "Desc");
|
|
48
|
-
assert.ok(openapi.paths["/ping"]);
|
|
49
|
-
assert.ok(openapi.components);
|
|
50
|
-
assert.ok(openapi.components.schemas.PingResponse);
|
|
51
|
-
assert.ok(Array.isArray(openapi.security));
|
|
52
|
-
assert.ok(Array.isArray(openapi.tags));
|
|
53
|
-
assert.equal(openapi["x-extra-rest-field"], true);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("toGraphQLSDL returns SDL string wrapper", () => {
|
|
57
|
-
const doc = {
|
|
58
|
-
unispecVersion: "0.1.0",
|
|
59
|
-
service: {
|
|
60
|
-
name: "test-service",
|
|
61
|
-
title: "GraphQL Service",
|
|
62
|
-
description: "GraphQL description",
|
|
63
|
-
protocols: {
|
|
64
|
-
graphql: {
|
|
65
|
-
schema: {
|
|
66
|
-
sdl: "type Query { hello: String! }",
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const result = toGraphQLSDL(doc);
|
|
74
|
-
|
|
75
|
-
assert.equal(typeof result, "object");
|
|
76
|
-
assert.equal(typeof result.sdl, "string");
|
|
77
|
-
assert.equal(result.sdl, "type Query { hello: String! }");
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("toWebSocketModel returns WS model wrapper", () => {
|
|
81
|
-
const doc = {
|
|
82
|
-
unispecVersion: "0.1.0",
|
|
83
|
-
service: {
|
|
84
|
-
name: "test-service",
|
|
85
|
-
title: "WS Service",
|
|
86
|
-
description: "WebSocket description",
|
|
87
|
-
protocols: {
|
|
88
|
-
websocket: {
|
|
89
|
-
channels: {
|
|
90
|
-
ping: {
|
|
91
|
-
summary: "Ping channel",
|
|
92
|
-
description: "Ping messages",
|
|
93
|
-
direction: "bidirectional",
|
|
94
|
-
messages: [
|
|
95
|
-
{
|
|
96
|
-
name: "ping",
|
|
97
|
-
summary: "Ping message",
|
|
98
|
-
},
|
|
99
|
-
],
|
|
100
|
-
},
|
|
101
|
-
},
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
const model = toWebSocketModel(doc);
|
|
108
|
-
|
|
109
|
-
assert.equal(typeof model, "object");
|
|
110
|
-
assert.ok(model["x-unispec-ws"]);
|
|
111
|
-
assert.deepEqual(model.service, {
|
|
112
|
-
name: "test-service",
|
|
113
|
-
title: "WS Service",
|
|
114
|
-
description: "WebSocket description",
|
|
115
|
-
});
|
|
116
|
-
assert.ok(Array.isArray(model.channels));
|
|
117
|
-
assert.equal(model.channels.length, 1);
|
|
118
|
-
assert.equal(model.channels[0].name, "ping");
|
|
119
|
-
assert.equal(model.channels[0].summary, "Ping channel");
|
|
120
|
-
assert.equal(model.channels[0].description, "Ping messages");
|
|
121
|
-
assert.equal(model.channels[0].direction, "bidirectional");
|
|
122
|
-
assert.ok(Array.isArray(model.channels[0].messages));
|
|
123
|
-
assert.equal(model.channels[0].messages[0].name, "ping");
|
|
124
|
-
assert.equal(model.channels[0].messages[0].summary, "Ping message");
|
|
125
|
-
assert.ok(model.rawProtocol);
|
|
126
|
-
});
|
package/tests/diff.test.mjs
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
|
|
4
|
-
import * as core from "../dist/index.js";
|
|
5
|
-
|
|
6
|
-
const { diffUniSpec } = core;
|
|
7
|
-
|
|
8
|
-
test("diffUniSpec detects added and removed fields", () => {
|
|
9
|
-
const oldDoc = { info: { title: "Old" } };
|
|
10
|
-
const newDoc = { info: { title: "New", description: "Desc" } };
|
|
11
|
-
|
|
12
|
-
const result = diffUniSpec(oldDoc, newDoc);
|
|
13
|
-
|
|
14
|
-
const paths = result.changes.map((c) => c.path).sort();
|
|
15
|
-
|
|
16
|
-
assert.ok(paths.includes("/info/title"));
|
|
17
|
-
assert.ok(paths.includes("/info/description"));
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("diffUniSpec marks REST path and operation changes with severity and kind", () => {
|
|
21
|
-
const oldDoc = {
|
|
22
|
-
unispecVersion: "0.1.0",
|
|
23
|
-
service: {
|
|
24
|
-
name: "test-service",
|
|
25
|
-
protocols: {
|
|
26
|
-
rest: {
|
|
27
|
-
paths: {
|
|
28
|
-
"/ping": {
|
|
29
|
-
get: {},
|
|
30
|
-
},
|
|
31
|
-
"/status": {
|
|
32
|
-
get: {},
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const newDoc = {
|
|
41
|
-
unispecVersion: "0.1.0",
|
|
42
|
-
service: {
|
|
43
|
-
name: "test-service",
|
|
44
|
-
protocols: {
|
|
45
|
-
rest: {
|
|
46
|
-
paths: {
|
|
47
|
-
"/ping": {
|
|
48
|
-
get: {},
|
|
49
|
-
post: {},
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const result = diffUniSpec(oldDoc, newDoc);
|
|
58
|
-
|
|
59
|
-
const restChanges = result.changes.filter((c) => c.protocol === "rest");
|
|
60
|
-
|
|
61
|
-
const pathRemoved = restChanges.find((c) => c.kind === "rest.path.removed");
|
|
62
|
-
const opAdded = restChanges.find((c) => c.kind === "rest.operation.added");
|
|
63
|
-
|
|
64
|
-
assert.ok(pathRemoved);
|
|
65
|
-
assert.equal(pathRemoved.severity, "breaking");
|
|
66
|
-
|
|
67
|
-
assert.ok(opAdded);
|
|
68
|
-
assert.equal(opAdded.severity, "non-breaking");
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("diffUniSpec marks WebSocket channel changes with severity and kind", () => {
|
|
72
|
-
const oldDoc = {
|
|
73
|
-
unispecVersion: "0.1.0",
|
|
74
|
-
service: {
|
|
75
|
-
name: "test-service",
|
|
76
|
-
protocols: {
|
|
77
|
-
websocket: {
|
|
78
|
-
channels: {
|
|
79
|
-
chat: {},
|
|
80
|
-
metrics: {},
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const newDoc = {
|
|
88
|
-
unispecVersion: "0.1.0",
|
|
89
|
-
service: {
|
|
90
|
-
name: "test-service",
|
|
91
|
-
protocols: {
|
|
92
|
-
websocket: {
|
|
93
|
-
channels: {
|
|
94
|
-
chat: {},
|
|
95
|
-
notifications: {},
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const result = diffUniSpec(oldDoc, newDoc);
|
|
103
|
-
|
|
104
|
-
const wsChanges = result.changes.filter((c) => c.protocol === "websocket");
|
|
105
|
-
|
|
106
|
-
const channelRemoved = wsChanges.find((c) => c.kind === "websocket.channel.removed");
|
|
107
|
-
const channelAdded = wsChanges.find((c) => c.kind === "websocket.channel.added");
|
|
108
|
-
|
|
109
|
-
assert.ok(channelRemoved);
|
|
110
|
-
assert.equal(channelRemoved.severity, "breaking");
|
|
111
|
-
|
|
112
|
-
assert.ok(channelAdded);
|
|
113
|
-
assert.equal(channelAdded.severity, "non-breaking");
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
test("diffUniSpec marks WebSocket message changes with severity and kind", () => {
|
|
117
|
-
const oldDoc = {
|
|
118
|
-
unispecVersion: "0.1.0",
|
|
119
|
-
service: {
|
|
120
|
-
name: "test-service",
|
|
121
|
-
protocols: {
|
|
122
|
-
websocket: {
|
|
123
|
-
channels: {
|
|
124
|
-
chat: {
|
|
125
|
-
messages: [
|
|
126
|
-
{ name: "messageA" },
|
|
127
|
-
{ name: "messageB" },
|
|
128
|
-
],
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
const newDoc = {
|
|
137
|
-
unispecVersion: "0.1.0",
|
|
138
|
-
service: {
|
|
139
|
-
name: "test-service",
|
|
140
|
-
protocols: {
|
|
141
|
-
websocket: {
|
|
142
|
-
channels: {
|
|
143
|
-
chat: {
|
|
144
|
-
messages: [
|
|
145
|
-
{ name: "messageA" },
|
|
146
|
-
{ name: "messageB" },
|
|
147
|
-
{ name: "messageC" },
|
|
148
|
-
],
|
|
149
|
-
},
|
|
150
|
-
},
|
|
151
|
-
},
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
const result = diffUniSpec(oldDoc, newDoc);
|
|
157
|
-
|
|
158
|
-
const wsChangesAdd = result.changes.filter((c) => c.protocol === "websocket" && c.kind === "websocket.message.added");
|
|
159
|
-
|
|
160
|
-
assert.ok(wsChangesAdd.length > 0);
|
|
161
|
-
wsChangesAdd.forEach((change) => {
|
|
162
|
-
assert.equal(change.severity, "non-breaking");
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const newDoc2 = {
|
|
166
|
-
unispecVersion: "0.1.0",
|
|
167
|
-
service: {
|
|
168
|
-
name: "test-service",
|
|
169
|
-
protocols: {
|
|
170
|
-
websocket: {
|
|
171
|
-
channels: {
|
|
172
|
-
chat: {
|
|
173
|
-
messages: [
|
|
174
|
-
{ name: "messageA" },
|
|
175
|
-
],
|
|
176
|
-
},
|
|
177
|
-
},
|
|
178
|
-
},
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
const result2 = diffUniSpec(oldDoc, newDoc2);
|
|
184
|
-
|
|
185
|
-
const wsChangesRemove = result2.changes.filter((c) => c.protocol === "websocket" && c.kind === "websocket.message.removed");
|
|
186
|
-
|
|
187
|
-
assert.ok(wsChangesRemove.length > 0);
|
|
188
|
-
wsChangesRemove.forEach((change) => {
|
|
189
|
-
assert.equal(change.severity, "breaking");
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
test("diffUniSpec marks GraphQL operation changes with severity and kind", () => {
|
|
194
|
-
const oldDoc = {
|
|
195
|
-
unispecVersion: "0.1.0",
|
|
196
|
-
service: {
|
|
197
|
-
name: "test-service",
|
|
198
|
-
protocols: {
|
|
199
|
-
graphql: {
|
|
200
|
-
operations: {
|
|
201
|
-
queries: {
|
|
202
|
-
me: {},
|
|
203
|
-
ping: {},
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
},
|
|
208
|
-
},
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
const newDoc = {
|
|
212
|
-
unispecVersion: "0.1.0",
|
|
213
|
-
service: {
|
|
214
|
-
name: "test-service",
|
|
215
|
-
protocols: {
|
|
216
|
-
graphql: {
|
|
217
|
-
operations: {
|
|
218
|
-
queries: {
|
|
219
|
-
ping: {},
|
|
220
|
-
status: {},
|
|
221
|
-
},
|
|
222
|
-
},
|
|
223
|
-
},
|
|
224
|
-
},
|
|
225
|
-
},
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
const result = diffUniSpec(oldDoc, newDoc);
|
|
229
|
-
|
|
230
|
-
const graphqlChanges = result.changes.filter((c) => c.protocol === "graphql");
|
|
231
|
-
|
|
232
|
-
const opRemoved = graphqlChanges.find((c) => c.kind === "graphql.operation.removed");
|
|
233
|
-
const opAdded = graphqlChanges.find((c) => c.kind === "graphql.operation.added");
|
|
234
|
-
|
|
235
|
-
assert.ok(opRemoved);
|
|
236
|
-
assert.equal(opRemoved.severity, "breaking");
|
|
237
|
-
|
|
238
|
-
assert.ok(opAdded);
|
|
239
|
-
assert.equal(opAdded.severity, "non-breaking");
|
|
240
|
-
});
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
|
|
4
|
-
import * as core from "../dist/index.js";
|
|
5
|
-
|
|
6
|
-
const { loadUniSpec, validateUniSpec } = core;
|
|
7
|
-
|
|
8
|
-
test("loadUniSpec loads JSON string into object", async () => {
|
|
9
|
-
const input = '{"info": {"title": "Test"}}';
|
|
10
|
-
const doc = await loadUniSpec(input);
|
|
11
|
-
assert.equal(typeof doc, "object");
|
|
12
|
-
assert.equal(doc.info.title, "Test");
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("validateUniSpec returns ValidationResult shape", async () => {
|
|
16
|
-
const result = await validateUniSpec({});
|
|
17
|
-
assert.equal(typeof result.valid, "boolean");
|
|
18
|
-
assert.ok(Array.isArray(result.errors));
|
|
19
|
-
});
|