mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
test(server): plan 4 — collapse remaining test harnesses; triage legacy migration tests
This commit is contained in:
@@ -1,122 +1,169 @@
|
||||
/**
|
||||
* Unit tests for trimUserWhitespace — the backfill migration that normalises
|
||||
* leading/trailing whitespace in stored usernames and emails.
|
||||
* Unit tests for the backfill migration that normalises leading/trailing
|
||||
* whitespace in stored usernames and emails.
|
||||
* Tests TRIM-MIG-001 through TRIM-MIG-010.
|
||||
*
|
||||
* Ported off the legacy runner (Task 0/Task 5c triage: its production
|
||||
* equivalent is a frozen numbered migration) onto the real
|
||||
* `Migration20200101020800_trim_leading_trailing_whitespace_from_stored_usernames`
|
||||
* (the legacy `trimUserWhitespace` export in `db/migrations.ts` was never
|
||||
* called by any numbered migration — this one reimplements the same logic
|
||||
* inline, deliberately, since the legacy file is not importable from there):
|
||||
* migrate to the step immediately before it, seed rows with raw SQL, apply
|
||||
* just that one migration, assert.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import Database from 'better-sqlite3';
|
||||
import { trimUserWhitespace } from '../../../src/db/migrations';
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import type { MikroORM } from '@mikro-orm/sqlite';
|
||||
import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step';
|
||||
|
||||
function makeDb() {
|
||||
const db = new Database(':memory:');
|
||||
db.exec('PRAGMA foreign_keys = ON');
|
||||
db.exec(`
|
||||
CREATE TABLE users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT UNIQUE NOT NULL,
|
||||
email TEXT UNIQUE NOT NULL,
|
||||
password_hash TEXT NOT NULL DEFAULT 'x',
|
||||
role TEXT NOT NULL DEFAULT 'user'
|
||||
)
|
||||
`);
|
||||
return db;
|
||||
const TARGET = 'Migration20200101020800_trim_leading_trailing_whitespace_from_stored_usernames';
|
||||
|
||||
async function ormBeforeTarget(): Promise<MikroORM> {
|
||||
const orm = await createMigrationOrm();
|
||||
const names = await pendingNames(orm);
|
||||
const idx = names.indexOf(TARGET);
|
||||
expect(idx).toBeGreaterThan(0);
|
||||
await migrateTo(orm, names[idx - 1]);
|
||||
return orm;
|
||||
}
|
||||
|
||||
function insert(db: Database.Database, username: string, email: string): number {
|
||||
const r = db.prepare('INSERT INTO users (username, email) VALUES (?, ?)').run(username, email);
|
||||
return Number(r.lastInsertRowid);
|
||||
async function insert(orm: MikroORM, username: string, email: string): Promise<number> {
|
||||
await rawExec(orm, "INSERT INTO users (username, email, password_hash) VALUES (?, ?, 'x')", [username, email]);
|
||||
const rows = await rawQuery<{ id: number }>(orm, 'SELECT last_insert_rowid() as id');
|
||||
return rows[0].id;
|
||||
}
|
||||
|
||||
function row(db: Database.Database, id: number) {
|
||||
return db.prepare('SELECT username, email FROM users WHERE id = ?').get(id) as { username: string; email: string };
|
||||
async function row(orm: MikroORM, id: number): Promise<{ username: string; email: string }> {
|
||||
const rows = await rawQuery<{ username: string; email: string }>(orm, 'SELECT username, email FROM users WHERE id = ?', [id]);
|
||||
return rows[0];
|
||||
}
|
||||
|
||||
describe('trimUserWhitespace — clean data (no-op)', () => {
|
||||
it('TRIM-MIG-001 — leaves already-clean rows untouched', () => {
|
||||
const db = makeDb();
|
||||
const id = insert(db, 'alice', 'alice@example.com');
|
||||
trimUserWhitespace(db);
|
||||
expect(row(db, id)).toEqual({ username: 'alice', email: 'alice@example.com' });
|
||||
});
|
||||
it('TRIM-MIG-001 — leaves already-clean rows untouched', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
const id = await insert(orm, 'alice', 'alice@example.com');
|
||||
await migrateTo(orm, TARGET);
|
||||
expect(await row(orm, id)).toEqual({ username: 'alice', email: 'alice@example.com' });
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
});
|
||||
|
||||
describe('trimUserWhitespace — non-colliding dirty rows', () => {
|
||||
it('TRIM-MIG-002 — trims trailing whitespace from username', () => {
|
||||
const db = makeDb();
|
||||
const id = insert(db, 'alice ', 'alice@example.com');
|
||||
trimUserWhitespace(db);
|
||||
expect(row(db, id).username).toBe('alice');
|
||||
});
|
||||
it('TRIM-MIG-002 — trims trailing whitespace from username', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
const id = await insert(orm, 'alice ', 'alice@example.com');
|
||||
await migrateTo(orm, TARGET);
|
||||
expect((await row(orm, id)).username).toBe('alice');
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it('TRIM-MIG-003 — trims leading whitespace from username', () => {
|
||||
const db = makeDb();
|
||||
const id = insert(db, ' alice', 'alice@example.com');
|
||||
trimUserWhitespace(db);
|
||||
expect(row(db, id).username).toBe('alice');
|
||||
});
|
||||
it('TRIM-MIG-003 — trims leading whitespace from username', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
const id = await insert(orm, ' alice', 'alice@example.com');
|
||||
await migrateTo(orm, TARGET);
|
||||
expect((await row(orm, id)).username).toBe('alice');
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it('TRIM-MIG-004 — trims surrounding whitespace from email', () => {
|
||||
const db = makeDb();
|
||||
const id = insert(db, 'alice', ' alice@example.com ');
|
||||
trimUserWhitespace(db);
|
||||
expect(row(db, id).email).toBe('alice@example.com');
|
||||
});
|
||||
it('TRIM-MIG-004 — trims surrounding whitespace from email', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
const id = await insert(orm, 'alice', ' alice@example.com ');
|
||||
await migrateTo(orm, TARGET);
|
||||
expect((await row(orm, id)).email).toBe('alice@example.com');
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it('TRIM-MIG-005 — emits a console.warn for each trimmed row', () => {
|
||||
const db = makeDb();
|
||||
insert(db, 'bob ', 'bob@example.com');
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
trimUserWhitespace(db);
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining('[migration] Trimmed username'));
|
||||
warn.mockRestore();
|
||||
});
|
||||
it('TRIM-MIG-005 — emits a console.warn for each trimmed row', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
await insert(orm, 'bob ', 'bob@example.com');
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
await migrateTo(orm, TARGET);
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining('[migration] Trimmed username'));
|
||||
warn.mockRestore();
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
});
|
||||
|
||||
describe('trimUserWhitespace — username collision handling', () => {
|
||||
it('TRIM-MIG-006 — renames the dirty row to <trimmed>__migrated_<id> on collision', () => {
|
||||
const db = makeDb();
|
||||
insert(db, 'carol', 'carol@example.com');
|
||||
const dirtyId = insert(db, 'carol ', 'carol2@example.com');
|
||||
trimUserWhitespace(db);
|
||||
expect(row(db, dirtyId).username).toBe(`carol__migrated_${dirtyId}`);
|
||||
});
|
||||
it('TRIM-MIG-006 — renames the dirty row to <trimmed>__migrated_<id> on collision', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
await insert(orm, 'carol', 'carol@example.com');
|
||||
const dirtyId = await insert(orm, 'carol ', 'carol2@example.com');
|
||||
await migrateTo(orm, TARGET);
|
||||
expect((await row(orm, dirtyId)).username).toBe(`carol__migrated_${dirtyId}`);
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it('TRIM-MIG-007 — emits a WHITESPACE COLLISION warning for username collision', () => {
|
||||
const db = makeDb();
|
||||
insert(db, 'dan', 'dan@example.com');
|
||||
insert(db, 'dan ', 'dan2@example.com');
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
trimUserWhitespace(db);
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining('WHITESPACE COLLISION username'));
|
||||
warn.mockRestore();
|
||||
});
|
||||
it('TRIM-MIG-007 — emits a WHITESPACE COLLISION warning for username collision', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
await insert(orm, 'dan', 'dan@example.com');
|
||||
await insert(orm, 'dan ', 'dan2@example.com');
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
await migrateTo(orm, TARGET);
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining('WHITESPACE COLLISION username'));
|
||||
warn.mockRestore();
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it('TRIM-MIG-008 — the renamed value does not conflict with the existing clean row', () => {
|
||||
const db = makeDb();
|
||||
const cleanId = insert(db, 'eve', 'eve@example.com');
|
||||
const dirtyId = insert(db, 'eve ', 'eve2@example.com');
|
||||
trimUserWhitespace(db);
|
||||
expect(row(db, cleanId).username).toBe('eve');
|
||||
expect(row(db, dirtyId).username).toBe(`eve__migrated_${dirtyId}`);
|
||||
});
|
||||
it('TRIM-MIG-008 — the renamed value does not conflict with the existing clean row', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
const cleanId = await insert(orm, 'eve', 'eve@example.com');
|
||||
const dirtyId = await insert(orm, 'eve ', 'eve2@example.com');
|
||||
await migrateTo(orm, TARGET);
|
||||
expect((await row(orm, cleanId)).username).toBe('eve');
|
||||
expect((await row(orm, dirtyId)).username).toBe(`eve__migrated_${dirtyId}`);
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
});
|
||||
|
||||
describe('trimUserWhitespace — email collision handling', () => {
|
||||
it('TRIM-MIG-009 — renames dirty email as <local>__migrated_<id>@<domain> on collision', () => {
|
||||
const db = makeDb();
|
||||
insert(db, 'frank', 'frank@example.com');
|
||||
const dirtyId = insert(db, 'frank2', ' frank@example.com ');
|
||||
trimUserWhitespace(db);
|
||||
expect(row(db, dirtyId).email).toBe(`frank__migrated_${dirtyId}@example.com`);
|
||||
});
|
||||
it('TRIM-MIG-009 — renames dirty email as <local>__migrated_<id>@<domain> on collision', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
await insert(orm, 'frank', 'frank@example.com');
|
||||
const dirtyId = await insert(orm, 'frank2', ' frank@example.com ');
|
||||
await migrateTo(orm, TARGET);
|
||||
expect((await row(orm, dirtyId)).email).toBe(`frank__migrated_${dirtyId}@example.com`);
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it('TRIM-MIG-010 — emits a WHITESPACE COLLISION warning for email collision', () => {
|
||||
const db = makeDb();
|
||||
insert(db, 'grace', 'grace@example.com');
|
||||
insert(db, 'grace2', 'grace@example.com ');
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
trimUserWhitespace(db);
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining('WHITESPACE COLLISION email'));
|
||||
warn.mockRestore();
|
||||
});
|
||||
it('TRIM-MIG-010 — emits a WHITESPACE COLLISION warning for email collision', async () => {
|
||||
const orm = await ormBeforeTarget();
|
||||
try {
|
||||
await insert(orm, 'grace', 'grace@example.com');
|
||||
await insert(orm, 'grace2', 'grace@example.com ');
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
await migrateTo(orm, TARGET);
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining('WHITESPACE COLLISION email'));
|
||||
warn.mockRestore();
|
||||
} finally {
|
||||
await orm.close(true);
|
||||
}
|
||||
}, 30000);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user