create-seiro 0.1.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.
@@ -0,0 +1,129 @@
1
+ import { expect, test, beforeAll, afterAll } from "bun:test";
2
+ import { createClient } from "seiro/client";
3
+ import type { Commands, Queries, Events, User } from "./types";
4
+
5
+ const DATABASE_URL = "postgres://seiro:seiro@localhost:5433/seiro_test";
6
+ process.env.DATABASE_URL = DATABASE_URL;
7
+
8
+ // Dynamic import to use test database
9
+ const serverModule = await import("./server");
10
+
11
+ type TestClient = ReturnType<typeof createClient<Commands, Queries, Events>>;
12
+ let client: TestClient;
13
+
14
+ beforeAll(async () => {
15
+ // Wait for server to be ready
16
+ await new Promise((r) => setTimeout(r, 500));
17
+
18
+ client = createClient<Commands, Queries, Events>("ws://localhost:3000/ws");
19
+ await client.connect<User>();
20
+ });
21
+
22
+ afterAll(() => {
23
+ client.close();
24
+ });
25
+
26
+ test("register creates user and returns token", async () => {
27
+ const email = `test-${Date.now()}@example.com`;
28
+ let result: { token: string; user: User } | null = null;
29
+ let error: string | null = null;
30
+
31
+ client.cmd(
32
+ "auth.register",
33
+ { email, password: "password123" },
34
+ {
35
+ onSuccess: (r) => {
36
+ result = r;
37
+ },
38
+ onError: (e) => {
39
+ error = e;
40
+ },
41
+ },
42
+ );
43
+
44
+ await new Promise((r) => setTimeout(r, 300));
45
+
46
+ expect(error).toBeNull();
47
+ expect(result).not.toBeNull();
48
+ expect(result!.token).toBeDefined();
49
+ expect(result!.user.email).toBe(email);
50
+ });
51
+
52
+ test("login with valid credentials returns token", async () => {
53
+ const email = `login-${Date.now()}@example.com`;
54
+
55
+ // First register
56
+ await new Promise<void>((resolve) => {
57
+ client.cmd(
58
+ "auth.register",
59
+ { email, password: "password123" },
60
+ { onSuccess: () => resolve() },
61
+ );
62
+ });
63
+
64
+ // Then login
65
+ let result: { token: string; user: User } | null = null;
66
+ client.cmd(
67
+ "auth.login",
68
+ { email, password: "password123" },
69
+ {
70
+ onSuccess: (r) => {
71
+ result = r;
72
+ },
73
+ },
74
+ );
75
+
76
+ await new Promise((r) => setTimeout(r, 300));
77
+
78
+ expect(result).not.toBeNull();
79
+ expect(result!.token).toBeDefined();
80
+ expect(result!.user.email).toBe(email);
81
+ });
82
+
83
+ test("login with invalid credentials fails", async () => {
84
+ let error: string | null = null;
85
+
86
+ client.cmd(
87
+ "auth.login",
88
+ { email: "nonexistent@example.com", password: "wrong" },
89
+ {
90
+ onError: (e) => {
91
+ error = e;
92
+ },
93
+ },
94
+ );
95
+
96
+ await new Promise((r) => setTimeout(r, 300));
97
+
98
+ expect(error).not.toBeNull();
99
+ });
100
+
101
+ test("duplicate registration fails", async () => {
102
+ const email = `dup-${Date.now()}@example.com`;
103
+
104
+ // First registration
105
+ await new Promise<void>((resolve) => {
106
+ client.cmd(
107
+ "auth.register",
108
+ { email, password: "password123" },
109
+ { onSuccess: () => resolve() },
110
+ );
111
+ });
112
+
113
+ // Second registration should fail
114
+ let error: string | null = null;
115
+ client.cmd(
116
+ "auth.register",
117
+ { email, password: "password123" },
118
+ {
119
+ onError: (e) => {
120
+ error = e;
121
+ },
122
+ },
123
+ );
124
+
125
+ await new Promise((r) => setTimeout(r, 300));
126
+
127
+ expect(error).not.toBeNull();
128
+ expect(error!).toContain("already registered");
129
+ });
@@ -0,0 +1,44 @@
1
+ import postgres from "postgres";
2
+ import { createServer } from "seiro/server";
3
+ import type { Commands, Queries, Events } from "./types";
4
+ import * as auth from "./auth/server";
5
+
6
+ const DATABASE_URL =
7
+ process.env.DATABASE_URL ?? "postgres://seiro:seiro@localhost:5432/seiro";
8
+
9
+ const sql = postgres(DATABASE_URL);
10
+
11
+ const server = createServer<Commands, Queries, Events>({
12
+ port: 3000,
13
+ auth: {
14
+ verify: auth.verifyToken,
15
+ public: ["auth.register", "auth.login"],
16
+ },
17
+ healthCheck: async () => {
18
+ await sql`SELECT 1`;
19
+ return true;
20
+ },
21
+ });
22
+
23
+ // Register auth handlers
24
+ auth.register(server, sql);
25
+
26
+ // Serve static files and start
27
+ const indexHtml = await Bun.file("index.html").text();
28
+
29
+ await server.start({
30
+ "/": new Response(indexHtml, {
31
+ headers: { "Content-Type": "text/html" },
32
+ }),
33
+ "/app.js": async () => {
34
+ const result = await Bun.build({
35
+ entrypoints: ["./app.ts"],
36
+ minify: false,
37
+ });
38
+ return new Response(result.outputs[0], {
39
+ headers: { "Content-Type": "application/javascript" },
40
+ });
41
+ },
42
+ });
43
+
44
+ console.log("Server running on http://localhost:3000");
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "strict": true,
7
+ "esModuleInterop": true,
8
+ "skipLibCheck": true,
9
+ "forceConsistentCasingInFileNames": true,
10
+ "noEmit": true,
11
+ "types": ["bun"],
12
+ "lib": ["ESNext", "DOM"]
13
+ },
14
+ "include": ["**/*.ts"],
15
+ "exclude": ["node_modules"]
16
+ }
@@ -0,0 +1,9 @@
1
+ // Re-export auth types
2
+ export type { User, AuthResult, AuthCommands, AuthQueries, AuthEvents } from "./auth/types";
3
+
4
+ // Combined types for the app
5
+ import type { AuthCommands, AuthQueries, AuthEvents } from "./auth/types";
6
+
7
+ export type Commands = AuthCommands;
8
+ export type Queries = AuthQueries;
9
+ export type Events = AuthEvents;