web-ui: init type API checks

This commit is contained in:
Johannes Kirschbauer
2024-06-05 11:07:40 +02:00
parent a8501282c1
commit 816217748a
5 changed files with 1098 additions and 11 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -5,10 +5,11 @@
"scripts": { "scripts": {
"start": "vite", "start": "vite",
"dev": "vite", "dev": "vite",
"build": "npm run check && vite build && npm run convert-html", "build": "npm run check && npm run test && vite build && npm run convert-html",
"convert-html": "node gtk.webview.js", "convert-html": "node gtk.webview.js",
"serve": "vite preview", "serve": "vite preview",
"check": "tsc --noEmit --skipLibCheck && eslint ./src" "check": "tsc --noEmit --skipLibCheck && eslint ./src",
"test": "vitest run --typecheck"
}, },
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
@@ -21,6 +22,7 @@
"daisyui": "^4.11.1", "daisyui": "^4.11.1",
"eslint": "^8.57.0", "eslint": "^8.57.0",
"eslint-plugin-tailwindcss": "^3.17.0", "eslint-plugin-tailwindcss": "^3.17.0",
"jsdom": "^24.1.0",
"json-schema-faker": "^0.5.6", "json-schema-faker": "^0.5.6",
"json-schema-to-ts": "^3.1.0", "json-schema-to-ts": "^3.1.0",
"postcss": "^8.4.38", "postcss": "^8.4.38",
@@ -31,7 +33,8 @@
"typescript": "^5.4.5", "typescript": "^5.4.5",
"typescript-eslint": "^7.10.0", "typescript-eslint": "^7.10.0",
"vite": "^5.0.11", "vite": "^5.0.11",
"vite-plugin-solid": "^2.8.2" "vite-plugin-solid": "^2.8.2",
"vitest": "^1.6.0"
}, },
"dependencies": { "dependencies": {
"material-icons": "^1.13.12", "material-icons": "^1.13.12",

View File

@@ -0,0 +1,56 @@
import { describe, it, expectTypeOf } from "vitest";
import { OperationNames, pyApi } from "@/src/message";
describe.concurrent("API types work properly", () => {
// Test some basic types
it("distinct success/error unions", async () => {
const k: OperationNames = "create_clan" as OperationNames; // Just a random key, since
expectTypeOf(pyApi[k].receive).toBeFunction();
expectTypeOf(pyApi[k].receive).parameter(0).toBeFunction();
// receive is a function that takes a function, which takes the response parameter
expectTypeOf(pyApi[k].receive)
.parameter(0)
.parameter(0)
.toMatchTypeOf<
{ status: "success"; data?: any } | { status: "error"; errors: any[] }
>();
});
it("Cannot access data of error response", async () => {
const k: OperationNames = "create_clan" as OperationNames; // Just a random key, since
expectTypeOf(pyApi[k].receive).toBeFunction();
expectTypeOf(pyApi[k].receive).parameter(0).toBeFunction();
expectTypeOf(pyApi[k].receive).parameter(0).parameter(0).toMatchTypeOf<
// @ts-expect-error: data is not defined in error responses
| { status: "success"; data?: any }
| { status: "error"; errors: any[]; data: any }
>();
});
it("Machine list receives a list of names/id string", async () => {
expectTypeOf(pyApi.list_machines.receive)
.parameter(0)
.parameter(0)
.toMatchTypeOf<
{ status: "success"; data: string[] } | { status: "error"; errors: any }
>();
});
it("Machine show receives an object with at least: machine_name, machine_description and machine_icon", async () => {
expectTypeOf(pyApi.show_machine.receive)
.parameter(0)
.parameter(0)
.toMatchTypeOf<
| {
status: "success";
data: {
machine_name: string;
machine_icon?: string | null;
machine_description?: string | null;
};
}
| { status: "error"; errors: any }
>();
});
});

View File

@@ -11,7 +11,7 @@
npmDeps = pkgs.fetchNpmDeps { npmDeps = pkgs.fetchNpmDeps {
src = ./app; src = ./app;
hash = "sha256-EadzSkIsV/cJtdxpIUvvpQhu5h3VyF8bLMpwfksNmWQ="; hash = "sha256-AwBrTnS/GAND/eogBic96kIQAJ4gdHorB8hEdpnGe5s=";
}; };
# The prepack script runs the build script, which we'd rather do in the build phase. # The prepack script runs the build script, which we'd rather do in the build phase.
npmPackFlags = [ "--ignore-scripts" ]; npmPackFlags = [ "--ignore-scripts" ];