chore: Fix the tests and add new types for raw

This commit is contained in:
BluJ
2023-04-05 13:51:43 -06:00
parent b8d6addf28
commit 45192e0358
19 changed files with 222 additions and 376 deletions

View File

@@ -1,9 +1,8 @@
import { UnionSelectKey, unionSelectKey } from "../config/config-types";
import { InputSpec, matchInputSpec } from "./output";
import { Parser } from "ts-matches";
import { UnionSelectKey, unionSelectKey, UnionValueKey, unionValueKey } from "../config/configTypes";
import { InputSpec, matchInputSpec, testListUnion } from "./output";
export type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T
? 1
: 2) extends <G>() => G extends U ? 1 : 2
export type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2
? Y
: N;
export function testOutput<A, B>(): (c: IfEquals<A, B>) => null {
@@ -13,11 +12,7 @@ export function testOutput<A, B>(): (c: IfEquals<A, B>) => null {
function isObject(item: unknown): item is object {
return !!(item && typeof item === "object" && !Array.isArray(item));
}
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (
x: infer R
) => any
? R
: never;
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
export function mergeDeep<A extends unknown[]>(...sources: A) {
return _mergeDeep({}, ...sources);
}
@@ -49,21 +44,11 @@ testOutput<InputSpec["rpc"]["enable"], boolean>()(null);
testOutput<InputSpec["rpc"]["username"], string>()(null);
testOutput<InputSpec["rpc"]["advanced"]["auth"], string[]>()(null);
testOutput<
InputSpec["rpc"]["advanced"]["serialversion"],
"segwit" | "non-segwit"
>()(null);
testOutput<InputSpec["rpc"]["advanced"]["serialversion"], "segwit" | "non-segwit">()(null);
testOutput<InputSpec["rpc"]["advanced"]["servertimeout"], number>()(null);
testOutput<InputSpec["advanced"]["peers"]["addnode"][0]["hostname"], string>()(
null
);
testOutput<InputSpec["testListUnion"][0][UnionSelectKey]["name"], string>()(
null
);
testOutput<
InputSpec["testListUnion"][0][UnionSelectKey][UnionSelectKey],
"lnd"
>()(null);
testOutput<InputSpec["advanced"]["peers"]["addnode"][0]["hostname"], string>()(null);
testOutput<InputSpec["testListUnion"][0]["union"][UnionValueKey]["name"], string>()(null);
testOutput<InputSpec["testListUnion"][0]["union"][UnionSelectKey], "lnd">()(null);
// prettier-ignore
// @ts-expect-error Expect that the string is the one above
testOutput<InputSpec["testListUnion"][0][UnionSelectKey][UnionSelectKey], "unionSelectKey">()(null);
@@ -73,7 +58,7 @@ describe("Inputs", () => {
const validInput: InputSpec = {
testListUnion: [
{
[unionSelectKey]: { [unionSelectKey]: "lnd", name: "string" },
union: { [unionSelectKey]: "lnd", [unionValueKey]: { name: "string" } },
},
],
rpc: {
@@ -112,7 +97,8 @@ describe("Inputs", () => {
},
dbcache: 5,
pruning: {
[unionSelectKey]: "disabled",
unionSelectKey: "disabled",
unionValueKey: {},
},
blockfilters: {
blockfilterindex: false,
@@ -121,19 +107,19 @@ describe("Inputs", () => {
bloomfilters: { peerbloomfilters: false },
},
};
test("Test just the input unions", () => {
testListUnion.validator().unsafeCast(validInput.testListUnion);
});
test("test valid input", () => {
const output = matchInputSpec.unsafeCast(validInput);
expect(output).toEqual(validInput);
});
test("test errors", () => {
expect(() =>
matchInputSpec.unsafeCast(
mergeDeep(validInput, { rpc: { advanced: { threads: 0 } } })
)
).toThrowError();
expect(() =>
matchInputSpec.unsafeCast(mergeDeep(validInput, { rpc: { enable: 2 } }))
matchInputSpec.unsafeCast(mergeDeep(validInput, { rpc: { advanced: { threads: 0 } } }))
).toThrowError();
expect(() => matchInputSpec.unsafeCast(mergeDeep(validInput, { rpc: { enable: 2 } }))).toThrowError();
expect(() =>
matchInputSpec.unsafeCast(
@@ -142,6 +128,5 @@ describe("Inputs", () => {
})
)
).toThrowError();
matchInputSpec.unsafeCast(validInput);
});
});