diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..dcae98c --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,63 @@ +name: CI + +on: + pull_request: + push: + branches: [main] + +permissions: + contents: read + +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + website: + runs-on: ubuntu-latest + timeout-minutes: 5 + services: + mysql: + image: mysql:8.0 + env: + MYSQL_ROOT_PASSWORD: fixture-only + MYSQL_DATABASE: afe + ports: + - 3306:3306 + options: >- + --health-cmd="mysqladmin ping -h 127.0.0.1 -pfixture-only" + --health-interval=5s + --health-timeout=5s + --health-retries=12 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version-file: .nvmrc + # main has no packageManager yet. Honor it when added; use pnpm 10 until then. + - name: Resolve pnpm version + id: pnpm + run: | + node --input-type=module -e ' + import { readFileSync, appendFileSync } from "node:fs"; + const { packageManager } = JSON.parse(readFileSync("package.json", "utf8")); + const version = packageManager?.match(/^pnpm@([^+]+)/)?.[1] ?? "10"; + appendFileSync(process.env.GITHUB_OUTPUT, `version=${version}\n`); + ' + - uses: pnpm/action-setup@v4 + with: + version: ${{ steps.pnpm.outputs.version }} + - uses: actions/setup-node@v4 + with: + node-version-file: .nvmrc + cache: pnpm + - run: pnpm install --frozen-lockfile + - name: Website tests (including both MySQL charsets) + working-directory: apps/website + # workshop-not-found.test.ts already fails on base 21268df: expected a + # public durable cache header, got no-store. Restore it once fixed. + run: | + mapfile -t tests < <(find src -name '*.test.ts' ! -path 'src/coursebuilder/workshop-not-found.test.ts') + pnpm exec node --test "${tests[@]}" + env: + AFE_TEST_MYSQL_URL: mysql://root:fixture-only@127.0.0.1:3306/afe diff --git a/apps/website/src/coursebuilder/user-profile-fields.ts b/apps/website/src/coursebuilder/user-profile-fields.ts index c64a783..379ec4a 100644 --- a/apps/website/src/coursebuilder/user-profile-fields.ts +++ b/apps/website/src/coursebuilder/user-profile-fields.ts @@ -1,20 +1,24 @@ /** ctv_User.name and ctv_User.image are varchar(255). */ const PROFILE_COLUMN_MAX = 255; -function fit(value: string | null | undefined) { - return typeof value === 'string' && value.length <= PROFILE_COLUMN_MAX - ? value - : null; +function fit(field: 'name' | 'image', value: string | null | undefined) { + if (typeof value !== 'string') return null; + const length = [...value].length; + if (length <= PROFILE_COLUMN_MAX) return value; + console.warn('Dropped oversized Clerk profile field', { field, length }); + return null; } /** * Cosmetic Clerk profile values for a CodeTV user row. A value too long for its * column is dropped, so a long avatar URL can never fail the write that links a * Clerk login to a guest-checkout purchase (and with it, Workshop Access). + * Count MySQL characters (code points), not UTF-16 units. The linker retries + * without cosmetic values if an older utf8mb3 table cannot store them. */ export function profileColumns(input: { name: string | null | undefined; image: string | null | undefined; }) { - return { name: fit(input.name), image: fit(input.image) }; + return { name: fit('name', input.name), image: fit('image', input.image) }; } diff --git a/apps/website/src/coursebuilder/users.mysql.test.ts b/apps/website/src/coursebuilder/users.mysql.test.ts new file mode 100644 index 0000000..d0cad80 --- /dev/null +++ b/apps/website/src/coursebuilder/users.mysql.test.ts @@ -0,0 +1,230 @@ +import assert from 'node:assert/strict'; +import { randomUUID } from 'node:crypto'; +import { mkdtemp, rm } from 'node:fs/promises'; +import { join } from 'node:path'; +import { createRequire } from 'node:module'; +import { it } from 'node:test'; +import { pathToFileURL } from 'node:url'; +import { build } from 'esbuild'; +import { eq } from 'drizzle-orm'; +import { drizzle } from 'drizzle-orm/mysql2'; +import mysql from 'mysql2/promise'; + +// Only an explicit loopback fixture URL is used, never DATABASE_URL. The URL's +// user must be able to create/drop a database; no existing tables are touched. +it( + 'links guest rows with Clerk profiles on MySQL 8 (utf8mb4 and utf8mb3)', + { + skip: + !process.env.AFE_TEST_MYSQL_URL && + 'Set AFE_TEST_MYSQL_URL to a local disposable MySQL 8 URL', + }, + async (t) => { + const url = new URL(process.env.AFE_TEST_MYSQL_URL!); + assert.ok( + ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname), + 'Only a loopback fixture is allowed', + ); + const databaseName = `afe114_${randomUUID().replaceAll('-', '')}`; + const admin = await mysql.createConnection(url.toString()); + const dir = await mkdtemp(join(process.cwd(), '.afe114-test-')); + let connection: + Awaited> | undefined; + let created = false; + try { + const [version] = await admin.query( + 'SELECT VERSION() AS version', + ); + assert.match(version[0].version, /^8\./, 'This proof requires MySQL 8'); + await admin.query( + `CREATE DATABASE \`${databaseName}\` CHARACTER SET utf8mb4`, + ); + created = true; + url.pathname = `/${databaseName}`; + url.searchParams.set('charset', 'utf8mb4'); + connection = await mysql.createConnection(url.toString()); + await connection.query( + "SET SESSION sql_mode = 'STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION'", + ); + + // Bundle only to resolve the app's extensionless TS imports. No mocks: + // the real linker and the real schema run against the injected MySQL db. + const outfile = join(dir, 'users.mjs'); + await build({ + stdin: { + contents: `export { getCourseBuilderUserForClerkUser } from './src/coursebuilder/users'; + export { users } from './src/db/schema';`, + resolveDir: process.cwd(), + loader: 'ts', + }, + outfile, + bundle: true, + platform: 'node', + format: 'esm', + packages: 'external', + }); + const { users, getCourseBuilderUserForClerkUser } = await import( + pathToFileURL(outfile).href + ); + const schema = { users }; + const db = drizzle(connection, { schema, mode: 'default' }); + // Generate DDL from schema.ts, including the stored JSON externalId. + // drizzle-kit 0.27's ESM entry uses dynamic require; use its CJS API. + const { generateMySQLDrizzleJson, generateMySQLMigration } = + createRequire(import.meta.url)( + 'drizzle-kit/api', + ) as typeof import('drizzle-kit/api'); + const empty = await generateMySQLDrizzleJson({}); + const current = await generateMySQLDrizzleJson(schema); + for (const statement of await generateMySQLMigration(empty, current)) { + await connection.query(statement); + } + + const cases: { + label: string; + name: string; + image: string | null; + expectedName: string | null; + expectedImage: string | null; + existingImage?: string; + existingName?: string; + }[] = [ + { + label: '256-char avatar', + name: 'Ada', + image: `https://img.clerk.com/${'a'.repeat(234)}`, + expectedName: 'Ada', + expectedImage: null, + }, + { + label: '261-char name', + name: 'n'.repeat(261), + image: null, + expectedName: null, + expectedImage: null, + }, + { + label: 'emoji within UTF-16 limit', + name: '😀'.repeat(100), + image: null, + expectedName: '😀'.repeat(100), + expectedImage: null, + }, + { + label: '128 emoji exceed UTF-16 but fit varchar', + name: '😀'.repeat(128), + image: null, + expectedName: '😀'.repeat(128), + expectedImage: null, + }, + { + label: '255 code points with an emoji', + name: `${'n'.repeat(254)}😀`, + image: null, + expectedName: `${'n'.repeat(254)}😀`, + expectedImage: null, + }, + { + label: 'keep existing short image', + name: 'Ada', + image: `https://img.clerk.com/${'a'.repeat(234)}`, + existingImage: 'https://example.com/kept.png', + expectedName: 'Ada', + expectedImage: 'https://example.com/kept.png', + }, + { + label: 'keep stored name when emoji avatar needs fallback', + name: 'Clerk Name', + existingName: 'Ada', + image: 'https://example.com/😀.png', + expectedName: 'Ada', + expectedImage: 'https://example.com/😀.png', + }, + { + label: 'normal user', + name: 'Ada Lovelace', + image: 'https://example.com/avatar.png', + expectedName: 'Ada Lovelace', + expectedImage: 'https://example.com/avatar.png', + }, + ]; + assert.equal(cases[0].image!.length, 256); + for (const charset of ['utf8mb4', 'utf8mb3']) { + // Do not convert the previous pass's valid utf8mb4 emoji rows. + await db.delete(users); + await connection.query( + `ALTER TABLE ctv_User CONVERT TO CHARACTER SET ${charset}`, + ); + for (const fixture of cases) { + await t.test(`${charset}: ${fixture.label}`, async () => { + const id = `guest_${randomUUID()}`; + const email = `${id}@example.test`; + const clerkId = `clerk_${randomUUID()}`; + await db.insert(users).values({ + id, + email, + name: fixture.existingName ?? null, + image: fixture.existingImage ?? null, + fields: { guest: true }, + }); + const guest = await db.query.users.findFirst({ + where: eq(users.id, id), + }); + assert.equal(guest?.externalId, null); + const user = await getCourseBuilderUserForClerkUser( + { + id: clerkId, + primaryEmailAddress: { emailAddress: email }, + fullName: fixture.name, + imageUrl: fixture.image, + }, + db, + ); + const needsCharsetFallback = + charset === 'utf8mb3' && + [fixture.expectedName, fixture.expectedImage].some( + (value) => + value !== null && /[\u{10000}-\u{10ffff}]/u.test(value), + ); + const expectedName = needsCharsetFallback + ? (fixture.existingName ?? null) + : fixture.expectedName; + const expectedImage = needsCharsetFallback + ? (fixture.existingImage ?? null) + : fixture.expectedImage; + assert.equal( + user?.id, + id, + 'must update the guest row, not insert another user', + ); + assert.equal(user?.externalId, clerkId); + assert.equal(user?.name, expectedName); + assert.equal(user?.image, expectedImage); + const stored = await db.query.users.findFirst({ + where: eq(users.id, id), + }); + assert.equal( + stored?.externalId, + clerkId, + 'generated column must link the Clerk login', + ); + assert.deepEqual(stored?.fields, { + guest: true, + externalId: clerkId, + }); + assert.equal(stored?.name, expectedName); + assert.equal(stored?.image, expectedImage); + }); + } + } + } finally { + try { + await connection?.end(); + if (created) await admin.query(`DROP DATABASE \`${databaseName}\``); + } finally { + await admin.end(); + await rm(dir, { recursive: true, force: true }); + } + } + }, +); diff --git a/apps/website/src/coursebuilder/users.ts b/apps/website/src/coursebuilder/users.ts index 3c157a3..08ab420 100644 --- a/apps/website/src/coursebuilder/users.ts +++ b/apps/website/src/coursebuilder/users.ts @@ -22,6 +22,38 @@ type ClerkCommerceUser = { emailAddresses?: ClerkEmailAddress[]; }; +// One fallback, not a retry loop: preserve legal utf8mb4 profiles, but never +// let cosmetic values prevent linking on an older utf8mb3 table. +async function writeWithProfileFallback( + profile: ReturnType, + write: (profile: ReturnType) => PromiseLike, + fallback: ReturnType = { name: null, image: null }, +) { + try { + await write(profile); + return profile; + } catch (error) { + // mysql2 exposes errno; PlanetScale embeds it in the Vitess message. + const cause = error instanceof Error && error.cause ? error.cause : error; + const errno = + Number((cause as { errno?: number })?.errno) || + Number( + (cause instanceof Error ? cause.message : '').match( + /\berrno\s*[:=]?\s*(1366|1406)\b/i, + )?.[1], + ); + if (errno !== 1366 && errno !== 1406) throw error; + console.warn('Retrying Clerk user write without cosmetic profile fields', { + fields: Object.entries(profile) + .filter(([, value]) => value !== null) + .map(([field, value]) => ({ field, length: [...value!].length })), + errno, + }); + await write(fallback); + return fallback; + } +} + function getDisplayName(user: ClerkCommerceUser) { return ( user.fullName ?? @@ -61,17 +93,18 @@ export async function findCourseBuilderUserForClerkUser( export async function getCourseBuilderUserForClerkUser( clerkUser: ClerkCommerceUser | null, + database = db, ) { if (!clerkUser?.id) return null; - const existingByExternalId = await db.query.users.findFirst({ + const existingByExternalId = await database.query.users.findFirst({ where: eq(users.externalId, clerkUser.id), }); if (existingByExternalId) return existingByExternalId; const email = getPrimaryEmail(clerkUser); - const existingByEmail = await db.query.users.findFirst({ + const existingByEmail = await database.query.users.findFirst({ where: eq(users.email, email), }); @@ -85,30 +118,45 @@ export async function getCourseBuilderUserForClerkUser( image: existingByEmail.image ?? clerkUser.imageUrl, }); - await db - .update(users) - .set({ fields, ...profile }) - .where(eq(users.id, existingByEmail.id)); - - return { ...existingByEmail, fields, ...profile }; + const savedProfile = await writeWithProfileFallback( + profile, + (values) => + database + .update(users) + .set({ fields, ...values }) + .where(eq(users.id, existingByEmail.id)), + { + name: existingByEmail.name ?? null, + image: existingByEmail.image ?? null, + }, + ); + + return { + ...existingByEmail, + fields, + ...savedProfile, + externalId: clerkUser.id, + }; } const id = `user_${randomUUID()}`; - await db.insert(users).values({ - id, - email, - ...profileColumns({ + await writeWithProfileFallback( + profileColumns({ name: getDisplayName(clerkUser), image: clerkUser.imageUrl, }), - fields: { - externalId: clerkUser.id, - }, - }); + (profile) => + database.insert(users).values({ + id, + email, + ...profile, + fields: { externalId: clerkUser.id }, + }), + ); return ( - (await db.query.users.findFirst({ + (await database.query.users.findFirst({ where: eq(users.id, id), })) ?? null );