File size: 4,748 Bytes
1f5ea39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * Unit tests for trimUserWhitespace β€” the backfill migration that normalises
 * leading/trailing whitespace in stored usernames and emails.
 * Tests TRIM-MIG-001 through TRIM-MIG-010.
 */
import { describe, it, expect, vi, beforeEach } from 'vitest';
import Database from 'better-sqlite3';
import { trimUserWhitespace } from '../../../src/db/migrations';

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;
}

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);
}

function row(db: Database.Database, id: number) {
  return db.prepare('SELECT username, email FROM users WHERE id = ?').get(id) as { username: string; email: string };
}

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' });
  });
});

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-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-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-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();
  });
});

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-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-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}`);
  });
});

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-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();
  });
});