mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
* changed tb imports * cleanup: replace console.log/warn with $console, remove commented-out code Removed various commented-out code and replaced direct `console.log` and `console.warn` usage across the codebase with `$console` from "core" for standardized logging. Also adjusted linting rules in biome.json to enable warnings for `console.log` usage. * ts: enable incremental * fix imports in test files reorganize imports to use "@sinclair/typebox" directly, replacing local utility references, and add missing "override" keywords in test classes. * added media permissions (#142) * added permissions support for media module introduced `MediaPermissions` for fine-grained access control in the media module, updated routes to enforce these permissions, and adjusted permission registration logic. * fix: handle token absence in getUploadHeaders and add tests for transport modes ensure getUploadHeaders does not set Authorization header when token is missing. Add unit tests to validate behavior for different token_transport options. * remove console.log on DropzoneContainer.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add bcrypt and refactored auth resolve (#147) * reworked auth architecture with improved password handling and claims Refactored password strategy to prepare supporting bcrypt, improving hashing/encryption flexibility. Updated authentication flow with enhanced user resolution mechanisms, safe JWT generation, and consistent profile handling. Adjusted dependencies to include bcryptjs and updated lock files accordingly. * fix strategy forms handling, add register route and hidden fields Refactored strategy forms to include hidden fields for type and name. Added a registration route with necessary adjustments to the admin controller and routes. Corrected field handling within relevant forms and components. * refactored auth handling to support bcrypt, extracted user pool * update email regex to allow '+' and '_' characters * update test stub password for AppAuth spec * update data exceptions to use HttpStatus constants, adjust logging level in AppUserPool * rework strategies to extend a base class instead of interface * added simple bcrypt test * add validation logs and improve data validation handling (#157) Added warning logs for invalid data during mutator validation, refined field validation logic to handle undefined values, and adjusted event validation comments for clarity. Minor improvements include exporting events from core and handling optional chaining in entity field validation. * modify MediaApi to support custom fetch implementation, defaults to native fetch (#158) * modify MediaApi to support custom fetch implementation, defaults to native fetch added an optional `fetcher` parameter to allow usage of a custom fetch function in both `upload` and `fetcher` methods. Defaults to the standard `fetch` if none is provided. * fix tests and improve api fetcher types * update admin basepath handling and window context integration (#155) Refactored `useBkndWindowContext` to include `admin_basepath` and updated its usage in routing. Improved type consistency with `AdminBkndWindowContext` and ensured default values are applied for window context. * trigger `repository-find-[one|many]-[before|after]` based on `limit` (#160) * refactor error handling in authenticator and password strategy (#161) made `respondWithError` method public, updated login and register routes in `PasswordStrategy` to handle errors using `respondWithError` for consistency. * add disableSubmitOnError prop to NativeForm and export getFlashMessage (#162) Introduced a `disableSubmitOnError` prop to NativeForm to control submit button behavior when errors are present. Also exported `getFlashMessage` from the core for external usage. * update dependencies in package.json (#156) moved several dependencies between devDependencies and dependencies for better categorization and removed redundant entries. * update imports to adjust nodeTestRunner path and remove unused export (#163) updated imports in test files to reflect the correct path for nodeTestRunner. removed redundant export of nodeTestRunner from index file to clean up module structure. In some environments this could cause issues requiring to exclude `node:test`, just removing it for now. * fix sync events not awaited (#164) * refactor(dropzone): extract DropzoneInner and unify state management with zustand (#165) Simplified Dropzone implementation by extracting inner logic to a new component, `DropzoneInner`. Replaced local dropzone state logic with centralized state management using zustand. Adjusted API exports and props accordingly for consistency and maintainability. * replace LiquidJs rendering with simplified renderer (#167) * replace LiquidJs rendering with simplified renderer Removed dependency on LiquidJS and replaced it with a custom templating solution using lodash `get`. Updated corresponding components, editors, and tests to align with the new rendering approach. Removed unused filters and tags. * remove liquid js from package json * feat/cli-generate-types (#166) * init types generation * update type generation for entities and fields Refactored `EntityTypescript` to support improved field types and relations. Added `toType` method overrides for various fields to define accurate TypeScript types. Enhanced CLI `types` command with new options for output style and file handling. Removed redundant test files. * update type generation code and CLI option description removed unused imports definition, adjusted formatting in EntityTypescript, and clarified the CLI style option description. * fix json schema field type generation * reworked system entities to prevent recursive types * reworked system entities to prevent recursive types * remove unused object function * types: use number instead of Generated * update data hooks and api types * update data hooks and api types * update data hooks and api types * update data hooks and api types --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
511 lines
16 KiB
TypeScript
511 lines
16 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
|
|
import { disableConsoleLog, enableConsoleLog, stripMark } from "core/utils";
|
|
import { Type } from "@sinclair/typebox";
|
|
import { Connection, entity, text } from "data";
|
|
import { Module } from "modules/Module";
|
|
import { type ConfigTable, getDefaultConfig, ModuleManager } from "modules/ModuleManager";
|
|
import { CURRENT_VERSION, TABLE_NAME } from "modules/migrations";
|
|
import { getDummyConnection } from "../helper";
|
|
import { diff } from "core/object/diff";
|
|
import type { Static } from "@sinclair/typebox";
|
|
|
|
describe("ModuleManager", async () => {
|
|
test("s1: no config, no build", async () => {
|
|
const { dummyConnection } = getDummyConnection();
|
|
|
|
const mm = new ModuleManager(dummyConnection);
|
|
|
|
// that is because no module is built
|
|
expect(mm.toJSON()).toEqual({ version: 0 } as any);
|
|
});
|
|
|
|
test("s2: no config, build", async () => {
|
|
const { dummyConnection } = getDummyConnection();
|
|
|
|
const mm = new ModuleManager(dummyConnection);
|
|
await mm.build();
|
|
|
|
expect(mm.version()).toBe(CURRENT_VERSION);
|
|
expect(mm.built()).toBe(true);
|
|
});
|
|
|
|
test("s3: config given, table exists, version matches", async () => {
|
|
const c = getDummyConnection();
|
|
const mm = new ModuleManager(c.dummyConnection);
|
|
await mm.build();
|
|
const version = mm.version();
|
|
const configs = mm.configs();
|
|
const json = stripMark({
|
|
...configs,
|
|
data: {
|
|
...configs.data,
|
|
basepath: "/api/data2",
|
|
entities: {
|
|
test: entity("test", {
|
|
content: text(),
|
|
}).toJSON(),
|
|
},
|
|
},
|
|
}) as any;
|
|
//const { version, ...json } = mm.toJSON() as any;
|
|
|
|
const c2 = getDummyConnection();
|
|
const db = c2.dummyConnection.kysely;
|
|
const mm2 = new ModuleManager(c2.dummyConnection, { initial: { version, ...json } });
|
|
await mm2.syncConfigTable();
|
|
await db
|
|
.insertInto(TABLE_NAME)
|
|
.values({ type: "config", json: JSON.stringify(json), version: CURRENT_VERSION })
|
|
.execute();
|
|
|
|
await mm2.build();
|
|
|
|
expect(json).toEqual(stripMark(mm2.configs()));
|
|
});
|
|
|
|
test("s3.1: (fetch) config given, table exists, version matches", async () => {
|
|
const configs = getDefaultConfig();
|
|
const json = {
|
|
...configs,
|
|
data: {
|
|
...configs.data,
|
|
basepath: "/api/data2",
|
|
entities: {
|
|
test: entity("test", {
|
|
content: text(),
|
|
}).toJSON(),
|
|
},
|
|
},
|
|
} as any;
|
|
//const { version, ...json } = mm.toJSON() as any;
|
|
|
|
const { dummyConnection } = getDummyConnection();
|
|
const db = dummyConnection.kysely;
|
|
const mm2 = new ModuleManager(dummyConnection);
|
|
await mm2.syncConfigTable();
|
|
// assume an initial version
|
|
await db.insertInto(TABLE_NAME).values({ type: "config", json: null, version: 1 }).execute();
|
|
await db
|
|
.insertInto(TABLE_NAME)
|
|
.values({ type: "config", json: JSON.stringify(json), version: CURRENT_VERSION })
|
|
.execute();
|
|
|
|
await mm2.build();
|
|
|
|
expect(stripMark(json)).toEqual(stripMark(mm2.configs()));
|
|
expect(mm2.configs().data.entities?.test).toBeDefined();
|
|
expect(mm2.configs().data.entities?.test?.fields?.content).toBeDefined();
|
|
expect(mm2.get("data").toJSON().entities?.test?.fields?.content).toBeDefined();
|
|
});
|
|
|
|
test("s4: config given, table exists, version outdated, migrate", async () => {
|
|
const c = getDummyConnection();
|
|
const mm = new ModuleManager(c.dummyConnection);
|
|
await mm.build();
|
|
const json = mm.configs();
|
|
|
|
const c2 = getDummyConnection();
|
|
const db = c2.dummyConnection.kysely;
|
|
const mm2 = new ModuleManager(c2.dummyConnection);
|
|
await mm2.syncConfigTable();
|
|
|
|
await db
|
|
.insertInto(TABLE_NAME)
|
|
.values({ json: JSON.stringify(json), type: "config", version: CURRENT_VERSION - 1 })
|
|
.execute();
|
|
|
|
await mm2.build();
|
|
});
|
|
|
|
test("s5: config given, table exists, version mismatch", async () => {
|
|
const c = getDummyConnection();
|
|
const mm = new ModuleManager(c.dummyConnection);
|
|
await mm.build();
|
|
const version = mm.version();
|
|
const json = mm.configs();
|
|
//const { version, ...json } = mm.toJSON() as any;
|
|
|
|
const c2 = getDummyConnection();
|
|
const db = c2.dummyConnection.kysely;
|
|
|
|
const mm2 = new ModuleManager(c2.dummyConnection, {
|
|
initial: { version: version - 1, ...json },
|
|
});
|
|
await mm2.syncConfigTable();
|
|
await db
|
|
.insertInto(TABLE_NAME)
|
|
.values({ type: "config", json: JSON.stringify(json), version: CURRENT_VERSION })
|
|
.execute();
|
|
|
|
expect(mm2.build()).rejects.toThrow(/version.*do not match/);
|
|
});
|
|
|
|
test("s6: no config given, table exists, fetch", async () => {
|
|
const c = getDummyConnection();
|
|
const mm = new ModuleManager(c.dummyConnection);
|
|
await mm.build();
|
|
const json = mm.configs();
|
|
//const { version, ...json } = mm.toJSON() as any;
|
|
|
|
const c2 = getDummyConnection();
|
|
const db = c2.dummyConnection.kysely;
|
|
|
|
const mm2 = new ModuleManager(c2.dummyConnection);
|
|
await mm2.syncConfigTable();
|
|
|
|
const config = {
|
|
...json,
|
|
data: {
|
|
...json.data,
|
|
basepath: "/api/data2",
|
|
},
|
|
};
|
|
await db
|
|
.insertInto(TABLE_NAME)
|
|
.values({ type: "config", json: JSON.stringify(config), version: CURRENT_VERSION })
|
|
.execute();
|
|
|
|
// run without config given
|
|
await mm2.build();
|
|
|
|
expect(mm2.configs().data.basepath).toBe("/api/data2");
|
|
});
|
|
|
|
/*test("blank app, modify config", async () => {
|
|
const { dummyConnection } = getDummyConnection();
|
|
|
|
const mm = new ModuleManager(dummyConnection);
|
|
await mm.build();
|
|
const configs = stripMark(mm.configs());
|
|
|
|
expect(mm.configs().server.admin.color_scheme).toBeUndefined();
|
|
expect(() => mm.get("server").schema().patch("admin", { color_scheme: "violet" })).toThrow();
|
|
await mm.get("server").schema().patch("admin", { color_scheme: "dark" });
|
|
await mm.save();
|
|
|
|
expect(mm.configs().server.admin.color_scheme).toBe("dark");
|
|
expect(stripMark(mm.configs())).toEqual({
|
|
...configs,
|
|
server: {
|
|
...configs.server,
|
|
admin: {
|
|
...configs.server.admin,
|
|
color_scheme: "dark",
|
|
},
|
|
},
|
|
});
|
|
});*/
|
|
|
|
test("partial config given", async () => {
|
|
const { dummyConnection } = getDummyConnection();
|
|
|
|
const partial = {
|
|
auth: {
|
|
enabled: true,
|
|
},
|
|
};
|
|
const mm = new ModuleManager(dummyConnection, {
|
|
initial: partial,
|
|
});
|
|
await mm.build();
|
|
|
|
expect(mm.version()).toBe(CURRENT_VERSION);
|
|
expect(mm.built()).toBe(true);
|
|
expect(mm.configs().auth.enabled).toBe(true);
|
|
expect(mm.configs().data.entities?.users).toBeDefined();
|
|
});
|
|
|
|
test("partial config given, but db version exists", async () => {
|
|
const c = getDummyConnection();
|
|
const mm = new ModuleManager(c.dummyConnection);
|
|
await mm.build();
|
|
console.log("==".repeat(30));
|
|
console.log("");
|
|
const json = mm.configs();
|
|
|
|
const c2 = getDummyConnection();
|
|
const db = c2.dummyConnection.kysely;
|
|
|
|
const mm2 = new ModuleManager(c2.dummyConnection, {
|
|
initial: {
|
|
auth: {
|
|
basepath: "/shouldnt/take/this",
|
|
},
|
|
},
|
|
});
|
|
await mm2.syncConfigTable();
|
|
const payload = {
|
|
...json,
|
|
auth: {
|
|
...json.auth,
|
|
enabled: true,
|
|
basepath: "/api/auth2",
|
|
},
|
|
};
|
|
await db
|
|
.insertInto(TABLE_NAME)
|
|
.values({
|
|
type: "config",
|
|
json: JSON.stringify(payload),
|
|
version: CURRENT_VERSION,
|
|
})
|
|
.execute();
|
|
await mm2.build();
|
|
expect(mm2.configs().auth.basepath).toBe("/api/auth2");
|
|
});
|
|
|
|
// @todo: add tests for migrations (check "backup" and new version)
|
|
|
|
describe("revert", async () => {
|
|
const failingModuleSchema = Type.Object({
|
|
value: Type.Optional(Type.Number()),
|
|
});
|
|
class FailingModule extends Module<typeof failingModuleSchema> {
|
|
getSchema() {
|
|
return failingModuleSchema;
|
|
}
|
|
|
|
override async build() {
|
|
//console.log("building FailingModule", this.config);
|
|
if (this.config.value && this.config.value < 0) {
|
|
throw new Error("value must be positive, given: " + this.config.value);
|
|
}
|
|
this.setBuilt();
|
|
}
|
|
}
|
|
class TestModuleManager extends ModuleManager {
|
|
constructor(...args: ConstructorParameters<typeof ModuleManager>) {
|
|
super(...args);
|
|
const [, options] = args;
|
|
// @ts-ignore
|
|
const initial = options?.initial?.failing ?? {};
|
|
this.modules["failing"] = new FailingModule(initial, this.ctx());
|
|
this.modules["failing"].setListener(async (c) => {
|
|
// @ts-ignore
|
|
await this.onModuleConfigUpdated("failing", c);
|
|
});
|
|
}
|
|
}
|
|
|
|
beforeEach(() => disableConsoleLog(["log", "warn", "error"]));
|
|
afterEach(enableConsoleLog);
|
|
|
|
test("it builds", async () => {
|
|
const { dummyConnection } = getDummyConnection();
|
|
const mm = new TestModuleManager(dummyConnection);
|
|
expect(mm).toBeDefined();
|
|
await mm.build();
|
|
expect(mm.toJSON()).toBeDefined();
|
|
});
|
|
|
|
test("it accepts config", async () => {
|
|
const { dummyConnection } = getDummyConnection();
|
|
const mm = new TestModuleManager(dummyConnection, {
|
|
initial: {
|
|
// @ts-ignore
|
|
failing: { value: 2 },
|
|
},
|
|
});
|
|
await mm.build();
|
|
expect(mm.configs()["failing"].value).toBe(2);
|
|
});
|
|
|
|
test("it crashes on invalid", async () => {
|
|
const { dummyConnection } = getDummyConnection();
|
|
const mm = new TestModuleManager(dummyConnection, {
|
|
initial: {
|
|
// @ts-ignore
|
|
failing: { value: -1 },
|
|
},
|
|
});
|
|
expect(mm.build()).rejects.toThrow(/value must be positive/);
|
|
expect(mm.configs()["failing"].value).toBe(-1);
|
|
});
|
|
|
|
test("it correctly accepts valid", async () => {
|
|
const mockOnUpdated = mock(() => null);
|
|
const { dummyConnection } = getDummyConnection();
|
|
const mm = new TestModuleManager(dummyConnection, {
|
|
onUpdated: async () => {
|
|
mockOnUpdated();
|
|
},
|
|
});
|
|
await mm.build();
|
|
// @ts-ignore
|
|
const f = mm.mutateConfigSafe("failing");
|
|
|
|
// @ts-ignore
|
|
expect(f.set({ value: 2 })).resolves.toBeDefined();
|
|
expect(mockOnUpdated).toHaveBeenCalled();
|
|
});
|
|
|
|
test("it reverts on safe mutate", async () => {
|
|
const mockOnUpdated = mock(() => null);
|
|
const { dummyConnection } = getDummyConnection();
|
|
const mm = new TestModuleManager(dummyConnection, {
|
|
initial: {
|
|
// @ts-ignore
|
|
failing: { value: 1 },
|
|
},
|
|
onUpdated: async () => {
|
|
mockOnUpdated();
|
|
},
|
|
});
|
|
await mm.build();
|
|
expect(mm.configs()["failing"].value).toBe(1);
|
|
|
|
// now safe mutate
|
|
// @ts-ignore
|
|
expect(mm.mutateConfigSafe("failing").set({ value: -2 })).rejects.toThrow(
|
|
/value must be positive/,
|
|
);
|
|
expect(mm.configs()["failing"].value).toBe(1);
|
|
expect(mockOnUpdated).toHaveBeenCalled();
|
|
});
|
|
|
|
test("it only accepts schema mutating methods", async () => {
|
|
const { dummyConnection } = getDummyConnection();
|
|
const mm = new TestModuleManager(dummyConnection);
|
|
await mm.build();
|
|
|
|
// @ts-ignore
|
|
const f = mm.mutateConfigSafe("failing");
|
|
|
|
// @ts-expect-error
|
|
expect(() => f.has("value")).toThrow();
|
|
// @ts-expect-error
|
|
expect(() => f.bypass()).toThrow();
|
|
// @ts-expect-error
|
|
expect(() => f.clone()).toThrow();
|
|
// @ts-expect-error
|
|
expect(() => f.get()).toThrow();
|
|
// @ts-expect-error
|
|
expect(() => f.default()).toThrow();
|
|
});
|
|
});
|
|
|
|
async function getRawConfig(c: Connection) {
|
|
return (await c.kysely
|
|
.selectFrom(TABLE_NAME)
|
|
.selectAll()
|
|
.where("type", "=", "config")
|
|
.orderBy("version", "desc")
|
|
.executeTakeFirstOrThrow()) as unknown as ConfigTable;
|
|
}
|
|
|
|
async function getDiffs(c: Connection, opts?: { dir?: "asc" | "desc"; limit?: number }) {
|
|
return await c.kysely
|
|
.selectFrom(TABLE_NAME)
|
|
.selectAll()
|
|
.where("type", "=", "diff")
|
|
.orderBy("version", opts?.dir ?? "desc")
|
|
.$if(!!opts?.limit, (b) => b.limit(opts!.limit!))
|
|
.execute();
|
|
}
|
|
|
|
describe("diffs", () => {
|
|
test("never empty", async () => {
|
|
const { dummyConnection: c } = getDummyConnection();
|
|
const mm = new ModuleManager(c);
|
|
await mm.build();
|
|
await mm.save();
|
|
expect(await getDiffs(c)).toHaveLength(0);
|
|
});
|
|
|
|
test("has timestamps", async () => {
|
|
const { dummyConnection: c } = getDummyConnection();
|
|
const mm = new ModuleManager(c);
|
|
await mm.build();
|
|
|
|
await mm.get("data").schema().patch("basepath", "/api/data2");
|
|
await mm.save();
|
|
|
|
const config = await getRawConfig(c);
|
|
const diffs = await getDiffs(c);
|
|
|
|
expect(config.json.data.basepath).toBe("/api/data2");
|
|
expect(diffs).toHaveLength(1);
|
|
expect(diffs[0]!.created_at).toBeDefined();
|
|
expect(diffs[0]!.updated_at).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe("validate & revert", () => {
|
|
const schema = Type.Object({
|
|
value: Type.Array(Type.Number(), { default: [] }),
|
|
});
|
|
type SampleSchema = Static<typeof schema>;
|
|
class Sample extends Module<typeof schema> {
|
|
getSchema() {
|
|
return schema;
|
|
}
|
|
override async build() {
|
|
this.setBuilt();
|
|
}
|
|
override async onBeforeUpdate(from: SampleSchema, to: SampleSchema) {
|
|
if (to.value.length > 3) {
|
|
throw new Error("too many values");
|
|
}
|
|
if (to.value.includes(7)) {
|
|
throw new Error("contains 7");
|
|
}
|
|
|
|
return to;
|
|
}
|
|
}
|
|
class TestModuleManager extends ModuleManager {
|
|
constructor(...args: ConstructorParameters<typeof ModuleManager>) {
|
|
super(...args);
|
|
this.modules["module1"] = new Sample({}, this.ctx());
|
|
}
|
|
}
|
|
test("respects module onBeforeUpdate", async () => {
|
|
const { dummyConnection: c } = getDummyConnection();
|
|
const mm = new TestModuleManager(c);
|
|
await mm.build();
|
|
|
|
const m = mm.get("module1" as any) as Sample;
|
|
|
|
{
|
|
expect(async () => {
|
|
await m.schema().set({ value: [1, 2, 3, 4, 5] });
|
|
return mm.save();
|
|
}).toThrow(/too many values/);
|
|
|
|
expect(m.config.value).toHaveLength(0);
|
|
expect((mm.configs() as any).module1.value).toHaveLength(0);
|
|
}
|
|
|
|
{
|
|
expect(async () => {
|
|
await mm.mutateConfigSafe("module1" as any).set({ value: [1, 2, 3, 4, 5] });
|
|
return mm.save();
|
|
}).toThrow(/too many values/);
|
|
|
|
expect(m.config.value).toHaveLength(0);
|
|
expect((mm.configs() as any).module1.value).toHaveLength(0);
|
|
}
|
|
|
|
{
|
|
expect(async () => {
|
|
await m.schema().set({ value: [1, 7, 5] });
|
|
return mm.save();
|
|
}).toThrow(/contains 7/);
|
|
|
|
expect(m.config.value).toHaveLength(0);
|
|
expect((mm.configs() as any).module1.value).toHaveLength(0);
|
|
}
|
|
|
|
{
|
|
expect(async () => {
|
|
await mm.mutateConfigSafe("module1" as any).set({ value: [1, 7, 5] });
|
|
return mm.save();
|
|
}).toThrow(/contains 7/);
|
|
|
|
expect(m.config.value).toHaveLength(0);
|
|
expect((mm.configs() as any).module1.value).toHaveLength(0);
|
|
}
|
|
});
|
|
});
|
|
});
|