create-donghanh 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-donghanh",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Scaffold a donghanh project — conversational UI with JSX",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -0,0 +1,83 @@
1
+ import { Database } from "bun:sqlite";
2
+ import { describe, expect, test } from "bun:test";
3
+ import { createExecutor } from "./executor";
4
+
5
+ function createTestDb(): D1Database {
6
+ const sqlite = new Database(":memory:");
7
+ sqlite.exec(`
8
+ CREATE TABLE items (
9
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
10
+ name TEXT NOT NULL,
11
+ created_at TEXT DEFAULT (datetime('now'))
12
+ )
13
+ `);
14
+ // Wrap bun:sqlite to match D1Database.prepare() interface
15
+ return {
16
+ prepare(query: string) {
17
+ const stmt = sqlite.prepare(query);
18
+ return {
19
+ bind(...params: unknown[]) {
20
+ return {
21
+ async run() {
22
+ stmt.run(...(params as any[]));
23
+ return { success: true, meta: {} };
24
+ },
25
+ async all() {
26
+ return { results: stmt.all(...(params as any[])), success: true };
27
+ },
28
+ async first() {
29
+ return stmt.get(...(params as any[])) ?? null;
30
+ },
31
+ };
32
+ },
33
+ async run() {
34
+ stmt.run();
35
+ return { success: true, meta: {} };
36
+ },
37
+ async all() {
38
+ return { results: stmt.all(), success: true };
39
+ },
40
+ async first() {
41
+ return stmt.get() ?? null;
42
+ },
43
+ } as any;
44
+ },
45
+ } as any;
46
+ }
47
+
48
+ const ctx = { userId: "test", request: new Request("http://localhost") };
49
+
50
+ describe("executor", () => {
51
+ test("list-items returns empty array", async () => {
52
+ const executor = createExecutor(createTestDb());
53
+ const result = (await executor("list-items", {}, ctx)) as any;
54
+ expect(result.items).toEqual([]);
55
+ });
56
+
57
+ test("add-item creates an item", async () => {
58
+ const executor = createExecutor(createTestDb());
59
+ const result = (await executor(
60
+ "add-item",
61
+ { name: "Buy milk" },
62
+ ctx,
63
+ )) as any;
64
+ expect(result.items).toHaveLength(1);
65
+ expect(result.items[0].name).toBe("Buy milk");
66
+ });
67
+
68
+ test("add-item then list-items", async () => {
69
+ const db = createTestDb();
70
+ const executor = createExecutor(db);
71
+ await executor("add-item", { name: "Item 1" }, ctx);
72
+ await executor("add-item", { name: "Item 2" }, ctx);
73
+ const result = (await executor("list-items", {}, ctx)) as any;
74
+ expect(result.items).toHaveLength(2);
75
+ });
76
+
77
+ test("unknown operation throws", () => {
78
+ const executor = createExecutor(createTestDb());
79
+ expect(executor("unknown", {}, ctx)).rejects.toThrow(
80
+ "Unknown operation: unknown",
81
+ );
82
+ });
83
+ });
@@ -6,7 +6,8 @@
6
6
  "scripts": {
7
7
  "dev": "wrangler dev",
8
8
  "deploy": "wrangler deploy",
9
- "db:init": "wrangler d1 execute my-donghanh-app-db --local --file=schema.sql"
9
+ "db:init": "wrangler d1 execute my-donghanh-app-db --local --file=schema.sql",
10
+ "test": "bun test"
10
11
  },
11
12
  "dependencies": {
12
13
  "@donghanh/core": "^0.1.1",
@@ -15,6 +16,7 @@
15
16
  },
16
17
  "devDependencies": {
17
18
  "@cloudflare/workers-types": "^4.0.0",
19
+ "@types/bun": "^1.0.0",
18
20
  "wrangler": "^4.0.0",
19
21
  "typescript": "^5.8.0"
20
22
  }
@@ -2,7 +2,7 @@
2
2
  "compilerOptions": {
3
3
  "target": "ES2017",
4
4
  "lib": ["esnext"],
5
- "types": ["@cloudflare/workers-types"],
5
+ "types": ["@cloudflare/workers-types", "bun-types"],
6
6
  "skipLibCheck": true,
7
7
  "strict": true,
8
8
  "noEmit": true,
@@ -1,28 +1,33 @@
1
- import { Hono } from "hono";
1
+ import { App, buildRegistry } from "@donghanh/core";
2
2
  import { gptRoutes } from "@donghanh/hono";
3
- import { buildRegistry, App } from "@donghanh/core";
4
- import { operations } from "./operations";
3
+ import { Hono } from "hono";
5
4
  import { createExecutor } from "./executor";
5
+ import { operations } from "./operations";
6
6
 
7
7
  type Env = { Bindings: { DB: D1Database } };
8
8
 
9
- const app = new Hono<Env>();
10
-
11
9
  const appNode = App({ operations });
12
10
  const registry = buildRegistry(appNode);
13
11
 
14
- app.route("/api", gptRoutes({
15
- registry,
16
- executor: (opId, vars, ctx) => createExecutor((ctx as any).db)(opId, vars, ctx),
17
- authenticate: async (request) => ({ userId: "anonymous" }),
18
- }));
12
+ const app = new Hono<Env>();
13
+
14
+ // Store DB reference for the executor
15
+ let _db: D1Database;
19
16
 
20
- // Inject D1 binding into executor context
21
- app.use("/api/*", async (c, next) => {
22
- (c as any).db = c.env.DB;
17
+ app.use("/*", async (c, next) => {
18
+ _db = c.env.DB;
23
19
  await next();
24
20
  });
25
21
 
22
+ app.route(
23
+ "/api",
24
+ gptRoutes({
25
+ registry,
26
+ executor: (opId, vars, ctx) => createExecutor(_db)(opId, vars, ctx),
27
+ authenticate: async () => ({ userId: "anonymous" }),
28
+ }),
29
+ );
30
+
26
31
  app.get("/", (c) => c.text("donghanh app running. Try GET /api/operations"));
27
32
 
28
33
  export default app;