| 1 |
/* |
| 2 |
* Copyright (C) 2024 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import { JsonValue } from 'cockpit'; |
| 7 |
import { |
| 8 |
import_json_object, get, get_optional, |
| 9 |
import_string, import_number, import_boolean, |
| 10 |
import_array, import_record, |
| 11 |
validate |
| 12 |
} from 'import-json'; |
| 13 |
|
| 14 |
import QUnit from 'qunit-tests'; |
| 15 |
|
| 16 |
// Hook into console.error |
| 17 |
|
| 18 |
let console_errors: string[] = []; |
| 19 |
const console_error = console.error; |
| 20 |
console.error = function (msg) { |
| 21 |
console_errors.push(msg); |
| 22 |
console_error(msg); |
| 23 |
}; |
| 24 |
|
| 25 |
QUnit.hooks.beforeEach(() => { |
| 26 |
console_errors = []; |
| 27 |
}); |
| 28 |
|
| 29 |
QUnit.test("import_string", function(assert) { |
| 30 |
assert.equal(import_string("foo"), "foo", "string"); |
| 31 |
assert.equal(import_string(""), "", "empty string"); |
| 32 |
assert.throws(() => import_string(12), /JSON validation error for : Not a string: 12/, "not a string"); |
| 33 |
}); |
| 34 |
|
| 35 |
QUnit.test("import_number", function(assert) { |
| 36 |
assert.equal(import_number(12), 12, "number"); |
| 37 |
assert.throws( |
| 38 |
() => import_number("foo"), |
| 39 |
/JSON validation error for : Not a number: "foo"/, |
| 40 |
"not a number"); |
| 41 |
}); |
| 42 |
|
| 43 |
QUnit.test("import_boolean", function(assert) { |
| 44 |
assert.equal(import_boolean(true), true, "boolean"); |
| 45 |
assert.throws( |
| 46 |
() => import_boolean("foo"), |
| 47 |
/JSON validation error for : Not a boolean: "foo"/, |
| 48 |
"not a boolean"); |
| 49 |
}); |
| 50 |
|
| 51 |
QUnit.test("import_array", function(assert) { |
| 52 |
assert.deepEqual(import_array(["a", "b", "c"], import_string), ["a", "b", "c"], "array of strings"); |
| 53 |
assert.deepEqual(import_array([1, 2, 3], import_number), [1, 2, 3], "array of numbers"); |
| 54 |
assert.deepEqual(import_array([], import_number), [], "empty array"); |
| 55 |
assert.throws( |
| 56 |
() => import_array([1, 2, "c"], import_number), |
| 57 |
/JSON validation error for \[2\]: Not a number: "c"/, |
| 58 |
"array of numbers with a string"); |
| 59 |
assert.throws( |
| 60 |
() => import_array("foo", import_string), |
| 61 |
/JSON validation error for : Not an array: "foo"/, |
| 62 |
"not an array"); |
| 63 |
}); |
| 64 |
|
| 65 |
QUnit.test("import_record", function(assert) { |
| 66 |
assert.deepEqual( |
| 67 |
import_record({ a: "a", b: "b", c: "c" }, import_string), |
| 68 |
{ a: "a", b: "b", c: "c" }, |
| 69 |
"record of strings"); |
| 70 |
assert.deepEqual( |
| 71 |
import_record({ a: 1, b: 2, c: 3 }, import_number), |
| 72 |
{ a: 1, b: 2, c: 3 }, |
| 73 |
"record of numbers"); |
| 74 |
assert.throws( |
| 75 |
() => import_record({ a: 1, b: 2, c: "c" }, import_number), |
| 76 |
/JSON validation error for \.c: Not a number: "c"/, |
| 77 |
"record of numbers with a string"); |
| 78 |
assert.throws( |
| 79 |
() => import_record("foo", import_string), |
| 80 |
/JSON validation error for : Not an object: "foo"/, |
| 81 |
"not a record"); |
| 82 |
}); |
| 83 |
|
| 84 |
QUnit.test("validate", function(assert) { |
| 85 |
assert.equal(validate("test input", "foo", import_string, "default"), "foo", "string"); |
| 86 |
assert.equal(validate("test input", 12, import_string, "default"), "default", "not a string"); |
| 87 |
assert.deepEqual( |
| 88 |
console_errors, |
| 89 |
[ |
| 90 |
'JSON validation error for test input: Not a string: 12' |
| 91 |
], |
| 92 |
"console errors" |
| 93 |
); |
| 94 |
}); |
| 95 |
|
| 96 |
interface Player { |
| 97 |
name: string; |
| 98 |
age: number | undefined; |
| 99 |
position: string; |
| 100 |
} |
| 101 |
|
| 102 |
function import_Player(val: JsonValue): Player { |
| 103 |
const obj = import_json_object(val); |
| 104 |
return { |
| 105 |
name: get(obj, "name", import_string), |
| 106 |
age: get_optional(obj, "age", import_number), |
| 107 |
position: get(obj, "position", import_string, "unknown"), |
| 108 |
}; |
| 109 |
} |
| 110 |
|
| 111 |
interface Team { |
| 112 |
name: string; |
| 113 |
players: Player[]; |
| 114 |
} |
| 115 |
|
| 116 |
function import_Team(val: JsonValue): Team { |
| 117 |
const obj = import_json_object(val); |
| 118 |
return { |
| 119 |
name: get(obj, "name", import_string), |
| 120 |
players: get(obj, "players", v => import_array(v, import_Player)), |
| 121 |
}; |
| 122 |
} |
| 123 |
|
| 124 |
interface Teams { |
| 125 |
[name: string]: Team; |
| 126 |
} |
| 127 |
|
| 128 |
function import_Teams(val: JsonValue): Teams { |
| 129 |
return import_record(val, import_Team); |
| 130 |
} |
| 131 |
|
| 132 |
QUnit.test("objects", function(assert) { |
| 133 |
const valid_checks: [JsonValue, Teams][] = [ |
| 134 |
// "stadium" should be omitted |
| 135 |
[ |
| 136 |
{ |
| 137 |
MAC: { |
| 138 |
name: "ManCity", |
| 139 |
players: [ |
| 140 |
{ name: "Haaland", age: 24, position: "striker" }, |
| 141 |
{ name: "De Bruyne", age: 33, position: "midfield" } |
| 142 |
], |
| 143 |
stadium: "City of Manchester Stadium" |
| 144 |
}, |
| 145 |
}, |
| 146 |
{ |
| 147 |
MAC: { |
| 148 |
name: "ManCity", |
| 149 |
players: [ |
| 150 |
{ name: "Haaland", age: 24, position: "striker" }, |
| 151 |
{ name: "De Bruyne", age: 33, position: "midfield" } |
| 152 |
], |
| 153 |
}, |
| 154 |
} |
| 155 |
|
| 156 |
], |
| 157 |
|
| 158 |
// optional fields |
| 159 |
[ |
| 160 |
{ |
| 161 |
MAC: { |
| 162 |
name: "ManCity", |
| 163 |
players: [ |
| 164 |
{ name: "Haaland", }, |
| 165 |
{ name: "De Bruyne", age: 33 } |
| 166 |
], |
| 167 |
}, |
| 168 |
}, |
| 169 |
{ |
| 170 |
MAC: { |
| 171 |
name: "ManCity", |
| 172 |
players: [ |
| 173 |
{ name: "Haaland", age: undefined, position: "unknown" }, |
| 174 |
{ name: "De Bruyne", age: 33, position: "unknown" } |
| 175 |
], |
| 176 |
}, |
| 177 |
} |
| 178 |
|
| 179 |
], |
| 180 |
]; |
| 181 |
|
| 182 |
const invalid_checks: [JsonValue, string][] = [ |
| 183 |
// invalid optional field |
| 184 |
[ |
| 185 |
{ |
| 186 |
MAC: { |
| 187 |
name: "ManCity", |
| 188 |
players: [ |
| 189 |
{ name: "Haaland", age: "unknown" }, |
| 190 |
{ name: "De Bruyne", age: 33 } |
| 191 |
], |
| 192 |
}, |
| 193 |
}, |
| 194 |
'JSON validation error for test input.MAC.players[0].age: Not a number: "unknown"', |
| 195 |
], |
| 196 |
|
| 197 |
// invalid mandatory field |
| 198 |
[ |
| 199 |
{ |
| 200 |
MAC: { |
| 201 |
name: "ManCity", |
| 202 |
players: [ |
| 203 |
{ name: ["Erling", "Braut", "Haaland"] }, |
| 204 |
{ name: "De Bruyne", age: 33 } |
| 205 |
], |
| 206 |
}, |
| 207 |
}, |
| 208 |
'JSON validation error for test input.MAC.players[0].name: Not a string: ["Erling","Braut","Haaland"]', |
| 209 |
], |
| 210 |
|
| 211 |
// invalid mandatory array |
| 212 |
[ |
| 213 |
{ |
| 214 |
MAC: { |
| 215 |
name: "ManCity", |
| 216 |
players: "TBD", |
| 217 |
}, |
| 218 |
}, |
| 219 |
'JSON validation error for test input.MAC.players: Not an array: "TBD"', |
| 220 |
], |
| 221 |
|
| 222 |
// invalid object |
| 223 |
[ |
| 224 |
"...", |
| 225 |
'JSON validation error for test input: Not an object: "..."' |
| 226 |
] |
| 227 |
]; |
| 228 |
|
| 229 |
for (let i = 0; i < valid_checks.length; i++) { |
| 230 |
assert.deepEqual(validate("test input", valid_checks[i][0], import_Teams, {}), valid_checks[i][1]); |
| 231 |
} |
| 232 |
|
| 233 |
for (let i = 0; i < invalid_checks.length; i++) { |
| 234 |
console_errors = []; |
| 235 |
assert.deepEqual(validate("test input", invalid_checks[i][0], import_Teams, {}), {}); |
| 236 |
assert.deepEqual(console_errors, [invalid_checks[i][1]]); |
| 237 |
} |
| 238 |
}); |
| 239 |
|
| 240 |
QUnit.start(); |