mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
fixing tests, move node tests to vitest
This commit is contained in:
@@ -1,17 +1,11 @@
|
||||
/// <reference types="bun-types" />
|
||||
|
||||
import path from "node:path";
|
||||
import {
|
||||
type RuntimeBkndConfig,
|
||||
createRuntimeApp,
|
||||
type RuntimeOptions,
|
||||
Connection,
|
||||
} from "bknd/adapter";
|
||||
import { type RuntimeBkndConfig, createRuntimeApp, type RuntimeOptions } from "bknd/adapter";
|
||||
import { registerLocalMediaAdapter } from ".";
|
||||
import { config } from "bknd/core";
|
||||
import type { ServeOptions } from "bun";
|
||||
import { serveStatic } from "hono/bun";
|
||||
import { sqlite } from "bknd/adapter/sqlite";
|
||||
import type { App } from "App";
|
||||
|
||||
type BunEnv = Bun.Env;
|
||||
@@ -25,17 +19,9 @@ export async function createApp<Env = BunEnv>(
|
||||
const root = path.resolve(distPath ?? "./node_modules/bknd/dist", "static");
|
||||
registerLocalMediaAdapter();
|
||||
|
||||
let connection: Connection | undefined;
|
||||
if (Connection.isConnection(config.connection)) {
|
||||
connection = config.connection;
|
||||
} else {
|
||||
connection = sqlite(config.connection ?? { url: ":memory:" });
|
||||
}
|
||||
|
||||
return await createRuntimeApp(
|
||||
{
|
||||
...config,
|
||||
connection,
|
||||
serveStatic: serveStatic({ root }),
|
||||
},
|
||||
args ?? (process.env as Env),
|
||||
|
||||
@@ -13,30 +13,32 @@ describe("cf adapter", () => {
|
||||
const DB_URL = ":memory:";
|
||||
const $ctx = (env?: any, request?: Request, ctx?: ExecutionContext) => ({
|
||||
request: request ?? (null as any),
|
||||
env: env ?? { DB_URL },
|
||||
env: env ?? { url: DB_URL },
|
||||
ctx: ctx ?? (null as any),
|
||||
});
|
||||
|
||||
it("makes config", async () => {
|
||||
expect(
|
||||
makeConfig(
|
||||
{
|
||||
connection: { url: DB_URL },
|
||||
},
|
||||
$ctx({ DB_URL }),
|
||||
),
|
||||
).toEqual({ connection: { url: DB_URL } });
|
||||
const staticConfig = makeConfig(
|
||||
{
|
||||
connection: { url: DB_URL },
|
||||
initialConfig: { data: { basepath: DB_URL } },
|
||||
},
|
||||
$ctx({ DB_URL }),
|
||||
);
|
||||
expect(staticConfig.initialConfig).toEqual({ data: { basepath: DB_URL } });
|
||||
expect(staticConfig.connection).toBeDefined();
|
||||
|
||||
expect(
|
||||
makeConfig(
|
||||
{
|
||||
app: (env) => ({
|
||||
connection: { url: env.DB_URL },
|
||||
}),
|
||||
},
|
||||
$ctx({ DB_URL }),
|
||||
),
|
||||
).toEqual({ connection: { url: DB_URL } });
|
||||
const dynamicConfig = makeConfig(
|
||||
{
|
||||
app: (env) => ({
|
||||
initialConfig: { data: { basepath: env.DB_URL } },
|
||||
connection: { url: env.DB_URL },
|
||||
}),
|
||||
},
|
||||
$ctx({ DB_URL }),
|
||||
);
|
||||
expect(dynamicConfig.initialConfig).toEqual({ data: { basepath: DB_URL } });
|
||||
expect(dynamicConfig.connection).toBeDefined();
|
||||
});
|
||||
|
||||
adapterTestSuite<CloudflareBkndConfig, CfMakeConfigArgs<any>>(bunTestRunner, {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { makeConfig as makeAdapterConfig } from "bknd/adapter";
|
||||
import type { Context, ExecutionContext } from "hono";
|
||||
import { $console } from "core";
|
||||
import { setCookie } from "hono/cookie";
|
||||
import { sqlite } from "bknd/adapter/sqlite";
|
||||
|
||||
export const constants = {
|
||||
exec_async_event_id: "cf_register_waituntil",
|
||||
@@ -98,54 +99,70 @@ export function makeConfig<Env extends CloudflareEnv = CloudflareEnv>(
|
||||
|
||||
const appConfig = makeAdapterConfig(config, args?.env);
|
||||
|
||||
if (args?.env) {
|
||||
const bindings = config.bindings?.(args?.env);
|
||||
// if connection instance is given, don't do anything
|
||||
// other than checking if D1 session is defined
|
||||
if (D1Connection.isConnection(appConfig.connection)) {
|
||||
if (config.d1?.session) {
|
||||
// we cannot guarantee that db was opened with session
|
||||
throw new Error(
|
||||
"D1 session don't work when D1 is directly given as connection. Define it in bindings instead.",
|
||||
);
|
||||
}
|
||||
// if connection is given, try to open with unified sqlite adapter
|
||||
} else if (appConfig.connection) {
|
||||
appConfig.connection = sqlite(appConfig.connection);
|
||||
|
||||
// if connection is not given, but env is set
|
||||
// try to make D1 from bindings
|
||||
} else if (args?.env) {
|
||||
const bindings = config.bindings?.(args?.env);
|
||||
const sessionHelper = d1SessionHelper(config);
|
||||
const sessionId = sessionHelper.get(args.request);
|
||||
let session: D1DatabaseSession | undefined;
|
||||
let db: D1Database | undefined;
|
||||
|
||||
if (!appConfig.connection) {
|
||||
let db: D1Database | undefined;
|
||||
if (bindings?.db) {
|
||||
$console.log("Using database from bindings");
|
||||
db = bindings.db;
|
||||
} else if (Object.keys(args).length > 0) {
|
||||
const binding = getBinding(args.env, "D1Database");
|
||||
if (binding) {
|
||||
$console.log(`Using database from env "${binding.key}"`);
|
||||
db = binding.value;
|
||||
}
|
||||
}
|
||||
// if db is given in bindings, use it
|
||||
if (bindings?.db) {
|
||||
$console.log("Using database from bindings");
|
||||
db = bindings.db;
|
||||
|
||||
if (db) {
|
||||
if (config.d1?.session) {
|
||||
session = db.withSession(sessionId ?? config.d1?.first);
|
||||
appConfig.connection = new D1Connection({ binding: session });
|
||||
} else {
|
||||
appConfig.connection = new D1Connection({ binding: db });
|
||||
}
|
||||
} else {
|
||||
throw new Error("No database connection given");
|
||||
// scan for D1Database in args
|
||||
} else {
|
||||
const binding = getBinding(args.env, "D1Database");
|
||||
if (binding) {
|
||||
$console.log(`Using database from env "${binding.key}"`);
|
||||
db = binding.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (config.d1?.session) {
|
||||
appConfig.options = {
|
||||
...appConfig.options,
|
||||
manager: {
|
||||
...appConfig.options?.manager,
|
||||
onServerInit: (server) => {
|
||||
server.use(async (c, next) => {
|
||||
sessionHelper.set(c, session);
|
||||
await next();
|
||||
});
|
||||
// if db is found, check if session is requested
|
||||
if (db) {
|
||||
if (config.d1?.session) {
|
||||
session = db.withSession(sessionId ?? config.d1?.first);
|
||||
|
||||
appConfig.connection = new D1Connection({ binding: session });
|
||||
appConfig.options = {
|
||||
...appConfig.options,
|
||||
manager: {
|
||||
...appConfig.options?.manager,
|
||||
onServerInit: (server) => {
|
||||
server.use(async (c, next) => {
|
||||
sessionHelper.set(c, session);
|
||||
await next();
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
} else {
|
||||
appConfig.connection = new D1Connection({ binding: db });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!D1Connection.isConnection(appConfig.connection)) {
|
||||
throw new Error("Couldn't find database connection");
|
||||
}
|
||||
|
||||
return appConfig;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
import { createWriteStream, readFileSync } from "node:fs";
|
||||
import { test } from "node:test";
|
||||
import { Miniflare } from "miniflare";
|
||||
import { StorageR2Adapter } from "./StorageR2Adapter";
|
||||
import { adapterTestSuite } from "media";
|
||||
import { nodeTestRunner } from "adapter/node/test";
|
||||
import path from "node:path";
|
||||
|
||||
// https://github.com/nodejs/node/issues/44372#issuecomment-1736530480
|
||||
console.log = async (message: any) => {
|
||||
const tty = createWriteStream("/dev/tty");
|
||||
const msg = typeof message === "string" ? message : JSON.stringify(message, null, 2);
|
||||
return tty.write(`${msg}\n`);
|
||||
};
|
||||
|
||||
test("StorageR2Adapter", async () => {
|
||||
const mf = new Miniflare({
|
||||
modules: true,
|
||||
script: "export default { async fetch() { return new Response(null); } }",
|
||||
r2Buckets: ["BUCKET"],
|
||||
});
|
||||
|
||||
const bucket = (await mf.getR2Bucket("BUCKET")) as unknown as R2Bucket;
|
||||
const adapter = new StorageR2Adapter(bucket);
|
||||
|
||||
const basePath = path.resolve(import.meta.dirname, "../../../../__test__/_assets");
|
||||
const buffer = readFileSync(path.join(basePath, "image.png"));
|
||||
const file = new File([buffer], "image.png", { type: "image/png" });
|
||||
|
||||
await adapterTestSuite(nodeTestRunner, adapter, file);
|
||||
await mf.dispose();
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { Miniflare } from "miniflare";
|
||||
import { StorageR2Adapter } from "./StorageR2Adapter";
|
||||
import { adapterTestSuite } from "media/storage/adapters/adapter-test-suite";
|
||||
import path from "node:path";
|
||||
import { describe, afterAll, test, expect } from "vitest";
|
||||
import { viTestRunner } from "adapter/node/vitest";
|
||||
|
||||
let mf: Miniflare | undefined;
|
||||
describe("StorageR2Adapter", async () => {
|
||||
mf = new Miniflare({
|
||||
modules: true,
|
||||
script: "export default { async fetch() { return new Response(null); } }",
|
||||
r2Buckets: ["BUCKET"],
|
||||
});
|
||||
const bucket = (await mf?.getR2Bucket("BUCKET")) as unknown as R2Bucket;
|
||||
|
||||
test("test", () => {
|
||||
expect(bucket).toBeDefined();
|
||||
});
|
||||
const adapter = new StorageR2Adapter(bucket);
|
||||
|
||||
const basePath = path.resolve(import.meta.dirname, "../../../../__test__/_assets");
|
||||
const buffer = readFileSync(path.join(basePath, "image.png"));
|
||||
const file = new File([buffer], "image.png", { type: "image/png" });
|
||||
|
||||
await adapterTestSuite(viTestRunner, adapter, file);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await mf?.dispose();
|
||||
});
|
||||
14
app/src/adapter/cloudflare/vitest.config.ts
Normal file
14
app/src/adapter/cloudflare/vitest.config.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
|
||||
|
||||
export default defineWorkersConfig({
|
||||
test: {
|
||||
poolOptions: {
|
||||
workers: {
|
||||
miniflare: {
|
||||
compatibilityDate: "2025-06-04",
|
||||
},
|
||||
},
|
||||
},
|
||||
include: ["**/*.vi-test.ts", "**/*.vitest.ts"],
|
||||
},
|
||||
});
|
||||
@@ -1,7 +1,8 @@
|
||||
import { App, type CreateAppConfig } from "bknd";
|
||||
import { config as $config } from "bknd/core";
|
||||
import { config as $config, $console } from "bknd/core";
|
||||
import type { MiddlewareHandler } from "hono";
|
||||
import type { AdminControllerOptions } from "modules/server/AdminController";
|
||||
import { Connection } from "bknd/data";
|
||||
|
||||
export { Connection } from "bknd/data";
|
||||
|
||||
@@ -61,7 +62,20 @@ export async function createAdapterApp<Config extends BkndConfig = BkndConfig, A
|
||||
const id = opts?.id ?? "app";
|
||||
let app = apps.get(id);
|
||||
if (!app || opts?.force) {
|
||||
app = App.create(makeConfig(config, args));
|
||||
const appConfig = makeConfig(config, args);
|
||||
if (!appConfig.connection || !Connection.isConnection(appConfig.connection)) {
|
||||
let connection: Connection | undefined;
|
||||
if (Connection.isConnection(config.connection)) {
|
||||
connection = config.connection;
|
||||
} else {
|
||||
const sqlite = (await import("bknd/adapter/sqlite")).sqlite;
|
||||
connection = sqlite(config.connection ?? { url: ":memory:" });
|
||||
$console.info(`Using ${connection.name} connection`);
|
||||
}
|
||||
appConfig.connection = connection;
|
||||
}
|
||||
|
||||
app = App.create(appConfig);
|
||||
apps.set(id, app);
|
||||
}
|
||||
return app;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { afterAll, beforeAll, describe } from "bun:test";
|
||||
import * as node from "./node.adapter";
|
||||
import { createApp, createHandler } from "./node.adapter";
|
||||
import { adapterTestSuite } from "adapter/adapter-test-suite";
|
||||
import { bunTestRunner } from "adapter/bun/test";
|
||||
import { disableConsoleLog, enableConsoleLog } from "core/utils";
|
||||
@@ -9,7 +9,7 @@ afterAll(enableConsoleLog);
|
||||
|
||||
describe("node adapter (bun)", () => {
|
||||
adapterTestSuite(bunTestRunner, {
|
||||
makeApp: node.createApp,
|
||||
makeHandler: node.createHandler,
|
||||
makeApp: createApp,
|
||||
makeHandler: createHandler,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
import path from "node:path";
|
||||
import { serve as honoServe } from "@hono/node-server";
|
||||
import { serveStatic } from "@hono/node-server/serve-static";
|
||||
import { registerLocalMediaAdapter } from "adapter/node/index";
|
||||
import {
|
||||
type RuntimeBkndConfig,
|
||||
createRuntimeApp,
|
||||
type RuntimeOptions,
|
||||
Connection,
|
||||
} from "bknd/adapter";
|
||||
import { registerLocalMediaAdapter } from "adapter/node/storage";
|
||||
import { type RuntimeBkndConfig, createRuntimeApp, type RuntimeOptions } from "bknd/adapter";
|
||||
import { config as $config } from "bknd/core";
|
||||
import { $console } from "core";
|
||||
import { sqlite } from "bknd/adapter/sqlite";
|
||||
import type { App } from "App";
|
||||
|
||||
type NodeEnv = NodeJS.ProcessEnv;
|
||||
@@ -35,18 +29,10 @@ export async function createApp<Env = NodeEnv>(
|
||||
console.warn("relativeDistPath is deprecated, please use distPath instead");
|
||||
}
|
||||
|
||||
let connection: Connection | undefined;
|
||||
if (Connection.isConnection(config.connection)) {
|
||||
connection = config.connection;
|
||||
} else {
|
||||
connection = sqlite(config.connection ?? { url: ":memory:" });
|
||||
}
|
||||
|
||||
registerLocalMediaAdapter();
|
||||
return await createRuntimeApp(
|
||||
{
|
||||
...config,
|
||||
connection,
|
||||
serveStatic: serveStatic({ root }),
|
||||
},
|
||||
// @ts-ignore
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe } from "node:test";
|
||||
import { nodeTestRunner } from "adapter/node/test";
|
||||
import { describe } from "vitest";
|
||||
import { viTestRunner } from "adapter/node/vitest";
|
||||
import { StorageLocalAdapter } from "adapter/node";
|
||||
import { adapterTestSuite } from "media/storage/adapters/adapter-test-suite";
|
||||
import { readFileSync } from "node:fs";
|
||||
@@ -14,5 +14,5 @@ describe("StorageLocalAdapter (node)", async () => {
|
||||
path: path.join(basePath, "tmp"),
|
||||
});
|
||||
|
||||
await adapterTestSuite(nodeTestRunner, adapter, file);
|
||||
await adapterTestSuite(viTestRunner, adapter, file);
|
||||
});
|
||||
@@ -1,8 +1,6 @@
|
||||
import type { Connection } from "bknd/data";
|
||||
import { $console } from "bknd/core";
|
||||
import { bunSqlite } from "../bun/connection/BunSqliteConnection";
|
||||
|
||||
export function sqlite(config: { url: string }): Connection {
|
||||
$console.info("Using bun-sqlite", config);
|
||||
return bunSqlite(config);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { $console } from "bknd/core";
|
||||
import type { Connection } from "bknd/data";
|
||||
import { libsql } from "../../data/connection/sqlite/LibsqlConnection";
|
||||
|
||||
export function sqlite(config: { url: string }): Connection {
|
||||
$console.info("Using libsql", config);
|
||||
return libsql(config);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { $console } from "bknd/core";
|
||||
import type { Connection } from "bknd/data";
|
||||
import { nodeSqlite } from "../node/connection/NodeSqliteConnection";
|
||||
|
||||
export function sqlite(config: { url: string }): Connection {
|
||||
$console.info("Using node-sqlite", config);
|
||||
return nodeSqlite(config);
|
||||
}
|
||||
|
||||
12
app/src/core/test/utils.ts
Normal file
12
app/src/core/test/utils.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { createApp as createAppInternal, type CreateAppConfig } from "App";
|
||||
import { bunSqlite } from "adapter/bun/connection/BunSqliteConnection";
|
||||
import { Connection } from "data/connection/Connection";
|
||||
|
||||
export { App } from "App";
|
||||
|
||||
export function createApp({ connection, ...config }: CreateAppConfig = {}) {
|
||||
return createAppInternal({
|
||||
...config,
|
||||
connection: Connection.isConnection(connection) ? connection : bunSqlite(connection as any),
|
||||
});
|
||||
}
|
||||
@@ -8,12 +8,14 @@ import {
|
||||
GenericSqliteDialect,
|
||||
} from "kysely-generic-sqlite";
|
||||
import { SqliteConnection } from "./SqliteConnection";
|
||||
import type { Features } from "../Connection";
|
||||
|
||||
export type GenericSqliteConnectionConfig = {
|
||||
name: string;
|
||||
additionalPlugins?: KyselyPlugin[];
|
||||
excludeTables?: string[];
|
||||
onCreateConnection?: OnCreateConnection;
|
||||
supports?: Partial<Features>;
|
||||
};
|
||||
|
||||
export { parseBigInt, buildQueryFn, GenericSqliteDialect, type IGenericSqlite };
|
||||
@@ -33,5 +35,15 @@ export class GenericSqliteConnection<DB = unknown> extends SqliteConnection<DB>
|
||||
excludeTables: config?.excludeTables,
|
||||
});
|
||||
this.client = db;
|
||||
if (config?.name) {
|
||||
this.name = config.name;
|
||||
}
|
||||
if (config?.supports) {
|
||||
for (const [key, value] of Object.entries(config.supports)) {
|
||||
if (value) {
|
||||
this.supported[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ import { connectionTestSuite } from "../connection-test-suite";
|
||||
import { LibsqlConnection } from "./LibsqlConnection";
|
||||
import { bunTestRunner } from "adapter/bun/test";
|
||||
import { describe } from "bun:test";
|
||||
import { createClient } from "@libsql/client";
|
||||
|
||||
describe("LibsqlConnection", () => {
|
||||
connectionTestSuite(bunTestRunner, {
|
||||
makeConnection: () => new LibsqlConnection({ url: ":memory:" }),
|
||||
makeConnection: () => new LibsqlConnection(createClient({ url: ":memory:" })),
|
||||
rawDialectDetails: ["rowsAffected", "lastInsertRowid"],
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,16 +11,9 @@ import {
|
||||
stripMark,
|
||||
transformObject,
|
||||
} from "core/utils";
|
||||
import {
|
||||
type Connection,
|
||||
EntityManager,
|
||||
type Schema,
|
||||
datetime,
|
||||
entity,
|
||||
enumm,
|
||||
jsonSchema,
|
||||
number,
|
||||
} from "data";
|
||||
import type { Connection, Schema } from "data";
|
||||
import { EntityManager } from "data/entities/EntityManager";
|
||||
import * as proto from "data/prototype";
|
||||
import { TransformPersistFailedException } from "data/errors";
|
||||
import { Hono } from "hono";
|
||||
import type { Kysely } from "kysely";
|
||||
@@ -116,12 +109,12 @@ const configJsonSchema = Type.Union([
|
||||
}),
|
||||
),
|
||||
]);
|
||||
export const __bknd = entity(TABLE_NAME, {
|
||||
version: number().required(),
|
||||
type: enumm({ enum: ["config", "diff", "backup"] }).required(),
|
||||
json: jsonSchema({ schema: configJsonSchema }).required(),
|
||||
created_at: datetime(),
|
||||
updated_at: datetime(),
|
||||
export const __bknd = proto.entity(TABLE_NAME, {
|
||||
version: proto.number().required(),
|
||||
type: proto.enumm({ enum: ["config", "diff", "backup"] }).required(),
|
||||
json: proto.jsonSchema({ schema: configJsonSchema }).required(),
|
||||
created_at: proto.datetime(),
|
||||
updated_at: proto.datetime(),
|
||||
});
|
||||
type ConfigTable2 = Schema<typeof __bknd>;
|
||||
interface T_INTERNAL_EM {
|
||||
|
||||
Reference in New Issue
Block a user