97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
import { SurrealAdapter } from "@suaveplan/surrealdb";
|
|
import {
|
|
afterAll,
|
|
beforeAll,
|
|
describe,
|
|
expect,
|
|
it,
|
|
} from "@suaveplan/testing/runner";
|
|
import { SurrealDBFixture } from "@suaveplan/testing-integration-surrealdb";
|
|
import { betterAuth } from "better-auth";
|
|
import { betterAuthSurrealAdapter } from "../../src/adapter/v1.6.js";
|
|
import { applyBetterAuthSchema } from "../../src/schema/schema.js";
|
|
|
|
let fixture: SurrealDBFixture | null = null;
|
|
let client: SurrealAdapter | null = null;
|
|
|
|
beforeAll(async () => {
|
|
fixture = new SurrealDBFixture();
|
|
const cfg = await fixture.start();
|
|
client = new SurrealAdapter({
|
|
url: cfg.url,
|
|
namespace: cfg.namespace,
|
|
database: cfg.database,
|
|
username: cfg.username,
|
|
password: cfg.password,
|
|
});
|
|
await client.connect();
|
|
// Bootstrap a Better-Auth-compatible `user` table. The bundled schema
|
|
// only extends user with `emailVerified`/`name`/`image`; Better-Auth's
|
|
// in-memory default user model also writes `createdAt`/`updatedAt` so
|
|
// those columns must exist before signUpEmail runs.
|
|
await client.execute("DEFINE TABLE OVERWRITE user SCHEMAFULL;");
|
|
await client.execute(
|
|
"DEFINE FIELD OVERWRITE email ON user TYPE option<string>;"
|
|
);
|
|
await client.execute(
|
|
"DEFINE FIELD OVERWRITE phoneNumber ON user TYPE option<string>;"
|
|
);
|
|
await client.execute(
|
|
"DEFINE FIELD OVERWRITE createdAt ON user TYPE option<datetime> DEFAULT time::now() VALUE $before OR time::now();"
|
|
);
|
|
await client.execute(
|
|
"DEFINE FIELD OVERWRITE updatedAt ON user TYPE option<datetime> DEFAULT time::now() VALUE time::now();"
|
|
);
|
|
await applyBetterAuthSchema(client);
|
|
}, 120_000);
|
|
|
|
afterAll(async () => {
|
|
if (client) {
|
|
await client.disconnect();
|
|
client = null;
|
|
}
|
|
if (fixture) {
|
|
await fixture.stop();
|
|
fixture = null;
|
|
}
|
|
}, 60_000);
|
|
|
|
describe("Better-Auth v1.6 end-to-end via @suaveplan/better-auth-surrealdb", () => {
|
|
it("createBetterAuth + signUpEmail round-trips against real SurrealDB", {
|
|
timeout: 60_000,
|
|
}, async () => {
|
|
if (client === null) {
|
|
throw new Error("client not ready");
|
|
}
|
|
const auth = betterAuth({
|
|
database: betterAuthSurrealAdapter({ client }),
|
|
secret: "test-secret-at-least-32-chars-long-for-better-auth",
|
|
baseURL: "http://localhost:3000",
|
|
emailAndPassword: { enabled: true },
|
|
});
|
|
|
|
const signUp = await auth.api.signUpEmail({
|
|
body: {
|
|
email: "e2e-user@example.com",
|
|
password: "Passw0rd!2026",
|
|
name: "E2E User",
|
|
},
|
|
});
|
|
|
|
expect(signUp.user).toBeDefined();
|
|
expect(signUp.user.email).toBe("e2e-user@example.com");
|
|
|
|
// Validate the user row landed in SurrealDB.
|
|
const users = await client.query<{ email: string }>(
|
|
"SELECT * FROM user WHERE email = 'e2e-user@example.com';"
|
|
);
|
|
expect(users.length).toBeGreaterThan(0);
|
|
|
|
// Validate an account row exists for the new user.
|
|
const accounts = await client.query(
|
|
"SELECT * FROM account WHERE providerId = 'credential';"
|
|
);
|
|
expect(accounts.length).toBeGreaterThan(0);
|
|
});
|
|
});
|