feat(custom-widgets): complete Custom JSX v2 (#6390)

This commit is contained in:
Thomas authored and ajnart committed 2026-08-13 19:48:57 +02:00
1 parent 3a1245cbc5
commit acdfcbb18e
416 files changed
+37853 -18066

No files matched your search

@@ -0,0 +1,65 @@
import { parse } from "superjson";
import { expect } from "vitest";
import { BUNDLED_CUSTOM_WIDGETS, customWidgetDefinitionSchema } from "@homarr/custom-widgets/core";
import type { Database } from "..";
import { eq } from "..";
import { seedDataAsync } from "../migrations/seed";
import { customWidgetDefinitions, customWidgetSecrets } from "../schema";
const expectedSeedIds = BUNDLED_CUSTOM_WIDGETS.map(({ id }) => id).toSorted();
const reconstructWidget = (definition: typeof customWidgetDefinitions.$inferSelect) =>
customWidgetDefinitionSchema.parse({
$schema: "homarr-custom-widget-v2",
name: definition.name,
...(definition.description === null ? {} : { description: definition.description }),
...(definition.iconUrl === null ? {} : { iconUrl: definition.iconUrl }),
sources: parse(definition.sources),
requests: parse(definition.requests),
options: parse(definition.options),
template: definition.template,
});
export const expectBundledCustomWidgetsSeeded = async (db: Database) => {
const definitions = await db.query.customWidgetDefinitions.findMany({
where: (table, { like }) => like(table.id, "seed-%"),
});
expect(definitions.map(({ id }) => id).toSorted()).toEqual(expectedSeedIds);
expect(definitions.every(({ enabled }) => !enabled)).toBe(true);
expect(definitions.every(({ creatorId }) => creatorId === null)).toBe(true);
expect(await db.$count(customWidgetSecrets)).toBe(0);
for (const definition of definitions) {
const expected = BUNDLED_CUSTOM_WIDGETS.find(({ id }) => id === definition.id);
expect(expected).toBeDefined();
expect(reconstructWidget(definition)).toEqual(customWidgetDefinitionSchema.parse(expected?.widget));
}
await db
.update(customWidgetDefinitions)
.set({ name: "User-edited dog facts", enabled: true })
.where(eq(customWidgetDefinitions.id, "seed-dog-facts"));
await db.delete(customWidgetDefinitions).where(eq(customWidgetDefinitions.id, "seed-currency-exchange"));
await seedDataAsync(db);
const reseededDefinitions = await db.query.customWidgetDefinitions.findMany({
where: (table, { like }) => like(table.id, "seed-%"),
});
const editedDefinition = reseededDefinitions.find(({ id }) => id === "seed-dog-facts");
const reseededCurrency = reseededDefinitions.find(({ id }) => id === "seed-currency-exchange");
expect(reseededDefinitions.map(({ id }) => id).toSorted()).toEqual(expectedSeedIds);
expect(editedDefinition).toMatchObject({ name: "User-edited dog facts", enabled: true, creatorId: null });
expect(reseededDefinitions.filter(({ id }) => id !== "seed-dog-facts").every(({ enabled }) => !enabled)).toBe(true);
if (!reseededCurrency) throw new Error("Currency Exchange seed was not restored");
expect(reconstructWidget(reseededCurrency)).toEqual(
customWidgetDefinitionSchema.parse(
BUNDLED_CUSTOM_WIDGETS.find(({ id }) => id === "seed-currency-exchange")?.widget,
),
);
expect(await db.$count(customWidgetSecrets)).toBe(0);
};
+5
View File
@@ -8,6 +8,9 @@ import { describe, test } from "vitest";
import { DB_CASING } from "@homarr/core/infrastructure/db/constants";
import * as mysqlSchema from "../schema/mysql";
import type { Database } from "..";
import { seedDataAsync } from "../migrations/seed";
import { expectBundledCustomWidgetsSeeded } from "./custom-widget-seed-assertions";
describe("Mysql Migration", () => {
test("should add all tables and keys specified in migration files", async () => {
@@ -31,9 +34,11 @@ describe("Mysql Migration", () => {
await migrate(database, {
migrationsFolder: path.join(__dirname, "..", "migrations", "mysql"),
});
await seedDataAsync(database as unknown as Database);
// Check if users table exists
await database.query.users.findMany();
await expectBundledCustomWidgetsSeeded(database as unknown as Database);
connection.end();
await mysqlContainer.stop();
@@ -8,6 +8,9 @@ import { describe, test } from "vitest";
import { DB_CASING } from "@homarr/core/infrastructure/db/constants";
import * as pgSchema from "../schema/postgresql";
import type { Database } from "..";
import { seedDataAsync } from "../migrations/seed";
import { expectBundledCustomWidgetsSeeded } from "./custom-widget-seed-assertions";
describe("PostgreSql Migration", () => {
test("should add all tables and keys specified in migration files", async () => {
@@ -36,9 +39,11 @@ describe("PostgreSql Migration", () => {
await migrate(database, {
migrationsFolder: path.join(__dirname, "..", "migrations", "postgresql"),
});
await seedDataAsync(database as unknown as Database);
// Check if users table exists
await database.query.users.findMany();
await expectBundledCustomWidgetsSeeded(database as unknown as Database);
// Close the pool to release resources
await pool.end();
+24
View File
@@ -0,0 +1,24 @@
import path from "path";
import BetterSqlite3 from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import { test } from "vitest";
import { DB_CASING } from "@homarr/core/infrastructure/db/constants";
import type { Database } from "..";
import { seedDataAsync } from "../migrations/seed";
import * as sqliteSchema from "../schema/sqlite";
import { expectBundledCustomWidgetsSeeded } from "./custom-widget-seed-assertions";
test("SQLite migrations seed the five disabled bundled custom widgets", async () => {
const connection = new BetterSqlite3(":memory:");
const database = drizzle(connection, { schema: sqliteSchema, casing: DB_CASING });
migrate(database, { migrationsFolder: path.join(__dirname, "..", "migrations", "sqlite") });
await seedDataAsync(database as unknown as Database);
await expectBundledCustomWidgetsSeeded(database as unknown as Database);
connection.close();
});