import * as Planetscale from "@alchemy.run/planetscale";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import { RuntimeContext } from "alchemy";
import * as Effect from "effect/Effect";
import * as Redacted from "effect/Redacted";
import { BetterAuth, Database } from "@/index.ts";
import { applyMigrations } from "@/Migrate.ts";
import { MySQL } from "@/MySQL.ts";
const { test } = Test.make({ providers: Planetscale.providers() });
const baseOptions = {
baseURL: "http://localhost:3000",
emailAndPassword: { enabled: true },
secret: "test-secret-test-secret-test-secret",
} as const;
test.provider(
"MySQL layer: migrate + auth roundtrip against PlanetScale",
(stack) =>
Effect.gen(function* () {
yield* stack.destroy();
const { database, password } = yield* stack.deploy(
Effect.gen(function* () {
const database = yield* Planetscale.MySQLDatabase("BetterAuthMySQL", {
clusterSize: "PS_10",
});
const password = yield* Planetscale.MySQLPassword("BetterAuthMySQLPassword", {
database,
role: "admin",
});
return { database, password };
}),
);
// PlanetScale requires TLS — mysql2 parses the `ssl` query param
const url =
`mysql://${password.username}:${Redacted.value(password.password)}` +
`@${password.host}/${database.name}` +
`?ssl=${encodeURIComponent('{"rejectUnauthorized":true}')}`;
const layer = MySQL(url);
const db = yield* Database.pipe(Effect.provide(layer));
expect(db.migrate).toBeDefined();
const first = yield* applyMigrations(db.migrate!, baseOptions);
expect(first.tablesCreated).toBeGreaterThan(0);
const second = yield* applyMigrations(db.migrate!, baseOptions);
expect(second.tablesCreated).toBe(0);
yield* Effect.scoped(
Effect.gen(function* () {
const auth = yield* BetterAuth(baseOptions);
const signUp = yield* auth.api.signUpEmail({
body: {
email: "planetscale@example.com",
password: "password1234",
name: "PlanetScale User",
},
});
expect(signUp.user.email).toBe("planetscale@example.com");
const signIn = yield* auth.api.signInEmail({
body: {
email: "planetscale@example.com",
password: "password1234",
},
});
expect(signIn.token).toBeDefined();
}).pipe(Effect.provide(layer)),
);
yield* stack.destroy();
}).pipe(Effect.provide(RuntimeContext.phantom)),
// Includes provisioning a fresh PlanetScale database.
{
tags: ["provider:planetscale", "provider:planetscale:mysql", "live"],
timeout: 240_000,
},
);