Skip to content

Commit 8077109

Browse files
Copilothotlong
andcommitted
Add tests and examples for plugin-better-auth
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent d113cd1 commit 8077109

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Example: Using the Better-Auth Plugin in ObjectOS
3+
*
4+
* This example demonstrates how to integrate the Better-Auth plugin
5+
* into an ObjectOS application.
6+
*/
7+
8+
import { ObjectOS } from '@objectos/kernel';
9+
import { BetterAuthPlugin, createBetterAuthPlugin } from '@objectos/plugin-better-auth';
10+
11+
/**
12+
* Example 1: Using the default plugin
13+
*/
14+
async function example1() {
15+
const os = new ObjectOS({
16+
plugins: [BetterAuthPlugin],
17+
});
18+
19+
await os.init();
20+
console.log('ObjectOS initialized with Better-Auth plugin (default config)');
21+
}
22+
23+
/**
24+
* Example 2: Using custom configuration
25+
*/
26+
async function example2() {
27+
const customAuthPlugin = createBetterAuthPlugin({
28+
// Use a specific database
29+
databaseUrl: process.env.DATABASE_URL || 'sqlite:auth.db',
30+
31+
// Set the base URL for auth endpoints
32+
baseURL: 'https://myapp.com/api/auth',
33+
34+
// Configure trusted origins for CORS
35+
trustedOrigins: [
36+
'https://myapp.com',
37+
'https://app.myapp.com',
38+
'http://localhost:3000'
39+
]
40+
});
41+
42+
const os = new ObjectOS({
43+
plugins: [customAuthPlugin],
44+
});
45+
46+
await os.init();
47+
console.log('ObjectOS initialized with Better-Auth plugin (custom config)');
48+
}
49+
50+
/**
51+
* Example 3: Using with other plugins
52+
*/
53+
async function example3() {
54+
// Import other plugins as needed
55+
// import { WorkflowPlugin } from '@objectos/plugin-workflow';
56+
// import { DataPlugin } from '@objectos/plugin-data';
57+
58+
const os = new ObjectOS({
59+
plugins: [
60+
BetterAuthPlugin,
61+
// WorkflowPlugin,
62+
// DataPlugin,
63+
],
64+
});
65+
66+
await os.init();
67+
console.log('ObjectOS initialized with multiple plugins');
68+
}
69+
70+
/**
71+
* Example 4: Accessing the auth instance from another plugin or service
72+
*/
73+
async function example4() {
74+
const { getBetterAuth } = await import('@objectos/plugin-better-auth');
75+
76+
// Get the auth instance (initialized by the plugin)
77+
const auth = await getBetterAuth();
78+
79+
// You can now use the auth instance directly
80+
console.log('Auth instance available for direct usage');
81+
}
82+
83+
// Run examples (uncomment to test)
84+
// example1();
85+
// example2();
86+
// example3();
87+
// example4();
88+
89+
export { example1, example2, example3, example4 };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
roots: ['<rootDir>/test'],
5+
testMatch: ['**/*.test.ts'],
6+
moduleFileExtensions: ['ts', 'js', 'json'],
7+
collectCoverageFrom: [
8+
'src/**/*.ts',
9+
'!src/**/*.d.ts'
10+
]
11+
};

packages/plugins/better-auth/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"@types/node": "^20.10.0",
2121
"@types/pg": "^8.11.0",
2222
"@types/better-sqlite3": "^7.6.0",
23+
"@types/jest": "^30.0.0",
24+
"jest": "^30.2.0",
25+
"ts-jest": "^29.4.6",
2326
"typescript": "^5.9.3"
2427
},
2528
"peerDependencies": {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Basic test for Better-Auth Plugin
3+
*/
4+
5+
import { BetterAuthPlugin, BetterAuthManifest, createBetterAuthPlugin } from '../src';
6+
7+
describe('Better-Auth Plugin', () => {
8+
it('should export BetterAuthPlugin', () => {
9+
expect(BetterAuthPlugin).toBeDefined();
10+
expect(typeof BetterAuthPlugin).toBe('object');
11+
});
12+
13+
it('should export BetterAuthManifest', () => {
14+
expect(BetterAuthManifest).toBeDefined();
15+
expect(BetterAuthManifest.id).toBe('com.objectos.auth.better-auth');
16+
expect(BetterAuthManifest.version).toBe('0.1.0');
17+
expect(BetterAuthManifest.type).toBe('plugin');
18+
});
19+
20+
it('should export createBetterAuthPlugin factory', () => {
21+
expect(createBetterAuthPlugin).toBeDefined();
22+
expect(typeof createBetterAuthPlugin).toBe('function');
23+
});
24+
25+
it('should create plugin with custom options', () => {
26+
const customPlugin = createBetterAuthPlugin({
27+
baseURL: 'https://example.com/auth',
28+
trustedOrigins: ['https://example.com']
29+
});
30+
expect(customPlugin).toBeDefined();
31+
expect(customPlugin.onInstall).toBeDefined();
32+
expect(customPlugin.onEnable).toBeDefined();
33+
expect(customPlugin.onDisable).toBeDefined();
34+
expect(customPlugin.onUninstall).toBeDefined();
35+
});
36+
37+
it('should have all lifecycle hooks', () => {
38+
expect(typeof BetterAuthPlugin.onInstall).toBe('function');
39+
expect(typeof BetterAuthPlugin.onEnable).toBe('function');
40+
expect(typeof BetterAuthPlugin.onDisable).toBe('function');
41+
expect(typeof BetterAuthPlugin.onUninstall).toBe('function');
42+
});
43+
44+
it('manifest should have correct contributes', () => {
45+
expect(BetterAuthManifest.contributes).toBeDefined();
46+
expect(BetterAuthManifest.contributes?.events).toBeDefined();
47+
expect(BetterAuthManifest.contributes?.events?.length).toBeGreaterThan(0);
48+
});
49+
50+
it('manifest should define authentication events', () => {
51+
const events = BetterAuthManifest.contributes?.events || [];
52+
expect(events).toContain('auth.user.created');
53+
expect(events).toContain('auth.user.login');
54+
expect(events).toContain('auth.user.logout');
55+
expect(events).toContain('auth.session.created');
56+
expect(events).toContain('auth.session.expired');
57+
});
58+
});

0 commit comments

Comments
 (0)