|
| 1 | +import { test } from "jsr:@denops/test@^3.0.0"; |
| 2 | +import { assertEquals, assertRejects } from "jsr:@std/assert@^1.0.0"; |
| 3 | +import { AssertError } from "jsr:@core/unknownutil@^4.3.0/assert"; |
| 4 | + |
| 5 | +import { main } from "./submatch.ts"; |
| 6 | + |
| 7 | +test({ |
| 8 | + mode: "all", |
| 9 | + name: "submatch - register dispatcher", |
| 10 | + fn: async (denops) => { |
| 11 | + main(denops); |
| 12 | + |
| 13 | + assertEquals(typeof denops.dispatcher["submatch"], "function"); |
| 14 | + }, |
| 15 | +}); |
| 16 | + |
| 17 | +test({ |
| 18 | + mode: "all", |
| 19 | + name: "submatch - reject invalid context", |
| 20 | + fn: async (denops) => { |
| 21 | + main(denops); |
| 22 | + |
| 23 | + await assertRejects( |
| 24 | + async () => { |
| 25 | + await denops.dispatcher["submatch"]({}, {}, {}); |
| 26 | + }, |
| 27 | + AssertError, |
| 28 | + ); |
| 29 | + }, |
| 30 | +}); |
| 31 | + |
| 32 | +test({ |
| 33 | + mode: "all", |
| 34 | + name: "submatch - reject invalid params", |
| 35 | + fn: async (denops) => { |
| 36 | + main(denops); |
| 37 | + |
| 38 | + const validContext = { |
| 39 | + filteredItems: [], |
| 40 | + _submatch: { |
| 41 | + pickerParams: { |
| 42 | + name: "test", |
| 43 | + source: { |
| 44 | + collect: async function* () { |
| 45 | + yield { value: "test" }; |
| 46 | + }, |
| 47 | + }, |
| 48 | + actions: { default: { invoke: async () => {} } }, |
| 49 | + defaultAction: "default", |
| 50 | + matchers: [{ |
| 51 | + match: async function* () { |
| 52 | + yield { value: "test", score: 1 }; |
| 53 | + }, |
| 54 | + }], |
| 55 | + }, |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + await assertRejects( |
| 60 | + async () => { |
| 61 | + await denops.dispatcher["submatch"](validContext, {}, {}); |
| 62 | + }, |
| 63 | + AssertError, |
| 64 | + ); |
| 65 | + }, |
| 66 | +}); |
| 67 | + |
| 68 | +test({ |
| 69 | + mode: "all", |
| 70 | + name: "submatch - accept valid call", |
| 71 | + fn: async (denops) => { |
| 72 | + main(denops); |
| 73 | + |
| 74 | + // Mock the picker dispatcher |
| 75 | + const originalDispatcher = denops.dispatcher; |
| 76 | + let pickerCalled = false; |
| 77 | + denops.dispatcher = { |
| 78 | + ...originalDispatcher, |
| 79 | + picker: () => { |
| 80 | + pickerCalled = true; |
| 81 | + return Promise.resolve(undefined); |
| 82 | + }, |
| 83 | + }; |
| 84 | + |
| 85 | + const validContext = { |
| 86 | + filteredItems: [{ value: "item1" }, { value: "item2" }], |
| 87 | + _submatch: { |
| 88 | + pickerParams: { |
| 89 | + name: "test", |
| 90 | + source: { |
| 91 | + collect: async function* () { |
| 92 | + yield { value: "test" }; |
| 93 | + }, |
| 94 | + }, |
| 95 | + actions: { default: { invoke: async () => {} } }, |
| 96 | + defaultAction: "default", |
| 97 | + matchers: [{ |
| 98 | + match: async function* () { |
| 99 | + yield { value: "test", score: 1 }; |
| 100 | + }, |
| 101 | + }], |
| 102 | + }, |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + const validParams = { |
| 107 | + matchers: [{ |
| 108 | + match: async function* () { |
| 109 | + yield { value: "test", score: 1 }; |
| 110 | + }, |
| 111 | + }], |
| 112 | + }; |
| 113 | + |
| 114 | + await denops.dispatcher["submatch"](validContext, validParams, {}); |
| 115 | + |
| 116 | + assertEquals(pickerCalled, true); |
| 117 | + |
| 118 | + // Restore |
| 119 | + denops.dispatcher = originalDispatcher; |
| 120 | + }, |
| 121 | +}); |
| 122 | + |
| 123 | +test({ |
| 124 | + mode: "all", |
| 125 | + name: "submatch - handle optional parameters", |
| 126 | + fn: async (denops) => { |
| 127 | + main(denops); |
| 128 | + |
| 129 | + // Mock the picker dispatcher |
| 130 | + const originalDispatcher = denops.dispatcher; |
| 131 | + let capturedParams: unknown; |
| 132 | + denops.dispatcher = { |
| 133 | + ...originalDispatcher, |
| 134 | + picker: (_args: unknown, params: unknown) => { |
| 135 | + capturedParams = params; |
| 136 | + return Promise.resolve(undefined); |
| 137 | + }, |
| 138 | + }; |
| 139 | + |
| 140 | + const validContext = { |
| 141 | + filteredItems: [{ value: "item1" }], |
| 142 | + selectedItems: [{ value: "selected" }], |
| 143 | + _submatch: { |
| 144 | + pickerParams: { |
| 145 | + name: "test", |
| 146 | + source: { |
| 147 | + collect: async function* () { |
| 148 | + yield { value: "test" }; |
| 149 | + }, |
| 150 | + }, |
| 151 | + actions: { default: { invoke: async () => {} } }, |
| 152 | + defaultAction: "default", |
| 153 | + matchers: [{ |
| 154 | + match: async function* () { |
| 155 | + yield { value: "test", score: 1 }; |
| 156 | + }, |
| 157 | + }], |
| 158 | + }, |
| 159 | + }, |
| 160 | + }; |
| 161 | + |
| 162 | + const validParams = { |
| 163 | + matchers: [{ |
| 164 | + match: async function* () { |
| 165 | + yield { value: "test", score: 1 }; |
| 166 | + }, |
| 167 | + }], |
| 168 | + actions: { custom: { invoke: async () => {} } }, |
| 169 | + defaultAction: "custom", |
| 170 | + sorters: [{ |
| 171 | + sort: async function* () { |
| 172 | + yield { value: "test", score: 1 }; |
| 173 | + }, |
| 174 | + }], |
| 175 | + renderers: [{ render: () => Promise.resolve([]) }], |
| 176 | + previewers: [{ preview: () => Promise.resolve([]) }], |
| 177 | + coordinator: { |
| 178 | + style: () => {}, |
| 179 | + layout: () => ({ width: 10, height: 10 }), |
| 180 | + }, |
| 181 | + theme: { |
| 182 | + border: ["a", "b", "c", "d", "e", "f", "g", "h"], |
| 183 | + divider: ["a", "b", "c", "d", "e", "f"], |
| 184 | + }, |
| 185 | + }; |
| 186 | + |
| 187 | + await denops.dispatcher["submatch"](validContext, validParams, {}); |
| 188 | + |
| 189 | + assertEquals(typeof capturedParams, "object"); |
| 190 | + assertEquals( |
| 191 | + (capturedParams as Record<string, unknown>).defaultAction, |
| 192 | + "custom", |
| 193 | + ); |
| 194 | + assertEquals( |
| 195 | + Array.isArray((capturedParams as Record<string, unknown>).sorters), |
| 196 | + true, |
| 197 | + ); |
| 198 | + assertEquals( |
| 199 | + Array.isArray((capturedParams as Record<string, unknown>).renderers), |
| 200 | + true, |
| 201 | + ); |
| 202 | + assertEquals( |
| 203 | + Array.isArray((capturedParams as Record<string, unknown>).previewers), |
| 204 | + true, |
| 205 | + ); |
| 206 | + assertEquals( |
| 207 | + typeof (capturedParams as Record<string, unknown>).coordinator, |
| 208 | + "object", |
| 209 | + ); |
| 210 | + assertEquals( |
| 211 | + typeof (capturedParams as Record<string, unknown>).theme, |
| 212 | + "object", |
| 213 | + ); |
| 214 | + |
| 215 | + // Restore |
| 216 | + denops.dispatcher = originalDispatcher; |
| 217 | + }, |
| 218 | +}); |
| 219 | + |
| 220 | +test({ |
| 221 | + mode: "all", |
| 222 | + name: "submatch - return true when picker returns true", |
| 223 | + fn: async (denops) => { |
| 224 | + main(denops); |
| 225 | + |
| 226 | + // Mock the picker dispatcher |
| 227 | + const originalDispatcher = denops.dispatcher; |
| 228 | + denops.dispatcher = { |
| 229 | + ...originalDispatcher, |
| 230 | + picker: () => { |
| 231 | + return Promise.resolve(true); |
| 232 | + }, |
| 233 | + }; |
| 234 | + |
| 235 | + const validContext = { |
| 236 | + filteredItems: [], |
| 237 | + _submatch: { |
| 238 | + pickerParams: { |
| 239 | + name: "test", |
| 240 | + source: { |
| 241 | + collect: async function* () { |
| 242 | + yield { value: "test" }; |
| 243 | + }, |
| 244 | + }, |
| 245 | + actions: { default: { invoke: async () => {} } }, |
| 246 | + defaultAction: "default", |
| 247 | + matchers: [{ |
| 248 | + match: async function* () { |
| 249 | + yield { value: "test", score: 1 }; |
| 250 | + }, |
| 251 | + }], |
| 252 | + }, |
| 253 | + }, |
| 254 | + }; |
| 255 | + |
| 256 | + const validParams = { |
| 257 | + matchers: [{ |
| 258 | + match: async function* () { |
| 259 | + yield { value: "test", score: 1 }; |
| 260 | + }, |
| 261 | + }], |
| 262 | + }; |
| 263 | + |
| 264 | + const result = await denops.dispatcher["submatch"]( |
| 265 | + validContext, |
| 266 | + validParams, |
| 267 | + {}, |
| 268 | + ); |
| 269 | + |
| 270 | + assertEquals(result, true); |
| 271 | + |
| 272 | + // Restore |
| 273 | + denops.dispatcher = originalDispatcher; |
| 274 | + }, |
| 275 | +}); |
0 commit comments