|
| 1 | +import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest"; |
| 2 | +import { CodeActionRequest } from "vscode-languageserver"; |
| 3 | +import { TestClient } from "../../test/test-client.ts"; |
| 4 | + |
| 5 | +describe("Feature - CodeAction: Extract subSchema to $defs", () => { |
| 6 | + let client: TestClient; |
| 7 | + let documentUri: string; |
| 8 | + |
| 9 | + beforeAll(async () => { |
| 10 | + client = new TestClient(); |
| 11 | + await client.start(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(async () => { |
| 15 | + await client.closeDocument(documentUri); |
| 16 | + }); |
| 17 | + |
| 18 | + afterAll(async () => { |
| 19 | + await client.stop(); |
| 20 | + }); |
| 21 | + |
| 22 | + test("Creating defs if not present", async () => { |
| 23 | + await client.writeDocument("subject.schema.json", `{ |
| 24 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 25 | + "type": "object", |
| 26 | + "properties": { |
| 27 | + "email": { |
| 28 | + "type": "string", |
| 29 | + "format": "email" |
| 30 | + } |
| 31 | + } |
| 32 | +}`); |
| 33 | + documentUri = await client.openDocument("subject.schema.json"); |
| 34 | + |
| 35 | + const codeActions = await client.sendRequest(CodeActionRequest.type, { |
| 36 | + textDocument: { uri: documentUri }, |
| 37 | + range: { |
| 38 | + start: { line: 4, character: 13 }, |
| 39 | + end: { line: 7, character: 5 } |
| 40 | + }, |
| 41 | + context: { diagnostics: [] } |
| 42 | + }); |
| 43 | + const expectedCodeAction = { |
| 44 | + edit: { |
| 45 | + documentChanges: [ |
| 46 | + { |
| 47 | + edits: [ |
| 48 | + { |
| 49 | + newText: `{ "$ref": "#/$defs/def1" }`, |
| 50 | + range: { |
| 51 | + end: { |
| 52 | + character: 5, |
| 53 | + line: 7 |
| 54 | + }, |
| 55 | + start: { |
| 56 | + character: 13, |
| 57 | + line: 4 |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + { |
| 62 | + newText: `, |
| 63 | + "$defs": { |
| 64 | + "def1": { |
| 65 | + "type": "string", |
| 66 | + "format": "email" |
| 67 | + } |
| 68 | + }`, |
| 69 | + range: { |
| 70 | + end: { |
| 71 | + character: 3, |
| 72 | + line: 8 |
| 73 | + }, |
| 74 | + start: { |
| 75 | + character: 3, |
| 76 | + line: 8 |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + ], |
| 81 | + textDocument: { |
| 82 | + uri: documentUri, |
| 83 | + version: null |
| 84 | + } |
| 85 | + } |
| 86 | + ] |
| 87 | + }, |
| 88 | + kind: "refactor.extract", |
| 89 | + title: "Extract 'def1' to $defs" |
| 90 | + }; |
| 91 | + expect(codeActions?.[0]).toMatchObject(expectedCodeAction); |
| 92 | + }); |
| 93 | + |
| 94 | + test("Handling existing defs with incremental names def1,def2", async () => { |
| 95 | + await client.writeDocument("subject.schema.json", `{ |
| 96 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 97 | + "type": "object", |
| 98 | + "properties": { |
| 99 | + "foo": { "$ref": "#/definitions/foo" }, |
| 100 | + "bar": { |
| 101 | + "type": "integer", |
| 102 | + "minimum": 0 |
| 103 | + } |
| 104 | + }, |
| 105 | + "definitions": { |
| 106 | + "def1": { |
| 107 | + "type": "string" |
| 108 | + } |
| 109 | + } |
| 110 | +
|
| 111 | +}`); |
| 112 | + documentUri = await client.openDocument("subject.schema.json"); |
| 113 | + |
| 114 | + const codeActions = await client.sendRequest(CodeActionRequest.type, { |
| 115 | + textDocument: { uri: documentUri }, |
| 116 | + range: { |
| 117 | + start: { line: 5, character: 11 }, |
| 118 | + end: { line: 8, character: 5 } |
| 119 | + }, |
| 120 | + context: { diagnostics: [] } |
| 121 | + }); |
| 122 | + const expectedCodeAction = { |
| 123 | + edit: { |
| 124 | + documentChanges: [ |
| 125 | + { |
| 126 | + edits: [ |
| 127 | + { |
| 128 | + newText: `{ "$ref": "#/definitions/def2" }`, |
| 129 | + range: { |
| 130 | + end: { |
| 131 | + character: 5, |
| 132 | + line: 8 |
| 133 | + }, |
| 134 | + start: { |
| 135 | + character: 11, |
| 136 | + line: 5 |
| 137 | + } |
| 138 | + } |
| 139 | + }, |
| 140 | + { |
| 141 | + newText: `, |
| 142 | + "def2": { |
| 143 | + "type": "integer", |
| 144 | + "minimum": 0 |
| 145 | + }`, |
| 146 | + range: { |
| 147 | + end: { |
| 148 | + character: 5, |
| 149 | + line: 13 |
| 150 | + }, |
| 151 | + start: { |
| 152 | + character: 5, |
| 153 | + line: 13 |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + ], |
| 158 | + textDocument: { |
| 159 | + uri: documentUri, |
| 160 | + version: null |
| 161 | + } |
| 162 | + } |
| 163 | + ] |
| 164 | + }, |
| 165 | + kind: "refactor.extract", |
| 166 | + title: "Extract 'def2' to definitions" |
| 167 | + }; |
| 168 | + expect(codeActions?.[0]).toMatchObject(expectedCodeAction); |
| 169 | + }); |
| 170 | + |
| 171 | + test("codeAction trigger when valid selection is made", async () => { |
| 172 | + await client.writeDocument("subject.schema.json", `{ |
| 173 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 174 | + "type": "object", |
| 175 | + "properties": { |
| 176 | + "email": { |
| 177 | + "type": "string", |
| 178 | + "format": "email" |
| 179 | + } |
| 180 | + } |
| 181 | +}`); |
| 182 | + documentUri = await client.openDocument("subject.schema.json"); |
| 183 | + |
| 184 | + const codeActions = await client.sendRequest(CodeActionRequest.type, { |
| 185 | + textDocument: { uri: documentUri }, |
| 186 | + range: { |
| 187 | + start: { line: 4, character: 13 }, |
| 188 | + end: { line: 7, character: 5 } |
| 189 | + }, |
| 190 | + context: { diagnostics: [] } |
| 191 | + }); |
| 192 | + expect(codeActions?.[0].title).toBe("Extract 'def1' to $defs"); |
| 193 | + }); |
| 194 | + test("no CodeAction trigger when selection is a single cursor point", async () => { |
| 195 | + await client.writeDocument("subject.schema.json", `{ |
| 196 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 197 | + "type": "object", |
| 198 | + "properties": { |
| 199 | + "email": { |
| 200 | + "type": "string", |
| 201 | + "format": "email" |
| 202 | + } |
| 203 | + } |
| 204 | +}`); |
| 205 | + documentUri = await client.openDocument("subject.schema.json"); |
| 206 | + |
| 207 | + const codeActions = await client.sendRequest(CodeActionRequest.type, { |
| 208 | + textDocument: { uri: documentUri }, |
| 209 | + range: { |
| 210 | + start: { line: 5, character: 6 }, |
| 211 | + end: { line: 5, character: 6 } |
| 212 | + }, |
| 213 | + context: { diagnostics: [] } |
| 214 | + }); |
| 215 | + expect(codeActions).to.eql([]); |
| 216 | + }); |
| 217 | +}); |
0 commit comments