Files
opencode-manager/backend/test/db/push-subscription-session-id-migration.test.ts
Chris Scott f8e4cfe136 feat(device-pairing): pair phones and browsers with a QR code
Settings -> Account gets "Connect a device", a paired-devices list, and a
remote access headers editor, so the mobile app and other browsers can sign
in without typing a password and pass a reverse proxy such as Cloudflare
Access.

- one-time pairing codes (3 min TTL) minted by a Better Auth plugin and
  redeemed into a new session that records the device name
- POST /api/device-pairing/links builds the ocm://link app link with the
  stored headers as h= parameters and a browser /pair link; plain-HTTP and
  loopback origins are blocked or warned about
- remote access headers are stored encrypted, returned by name only, and
  validated (8 headers, 512-char values, 1800 encoded characters, reserved
  transport names blocked); a Cloudflare Access service token preset fills
  CF-Access-Client-Id and CF-Access-Client-Secret
- login offers "Sign in with QR code" and /pair redeems a scanned code,
  returning to the login redirect target
- revoking a device deletes its session, closes its live connections and
  removes its push subscriptions; the current session cannot be revoked
2026-10-24 19:26:21 -04:00

74 lines
2.6 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { Database } from 'bun:sqlite'
import { migrate } from '../../src/db/migration-runner'
import { allMigrations } from '../../src/db/migrations'
const MIGRATION_ID = '202610231200-push-subscription-session-id'
const LEGACY_TABLE = `
CREATE TABLE push_subscriptions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL,
endpoint TEXT NOT NULL UNIQUE,
p256dh TEXT NOT NULL,
auth TEXT NOT NULL,
device_name TEXT,
created_at INTEGER NOT NULL,
last_used_at INTEGER
)
`
function columnNames(db: Database): string[] {
return (db.prepare('PRAGMA table_info(push_subscriptions)').all() as Array<{ name: string }>)
.map((column) => column.name)
}
function indexNames(db: Database): string[] {
return (db.prepare('PRAGMA index_list(push_subscriptions)').all() as Array<{ name: string }>)
.map((index) => index.name)
}
describe('push subscription session id migration', () => {
it('creates the push subscriptions table with its session id column', () => {
const db = new Database(':memory:')
migrate(db, allMigrations)
expect(columnNames(db)).toContain('session_id')
expect(indexNames(db)).toContain('idx_push_sub_session')
expect(indexNames(db)).toContain('idx_push_sub_endpoint')
db.close()
})
it('adds the session id column to an existing table without losing subscriptions', () => {
const db = new Database(':memory:')
db.run(LEGACY_TABLE)
db.prepare(
`INSERT INTO push_subscriptions (user_id, endpoint, p256dh, auth, device_name, created_at, last_used_at)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
).run('user-1', 'https://push.example.com/legacy', 'p256dh-key', 'auth-key', 'Old Phone', 1, 1)
migrate(db, allMigrations)
expect(columnNames(db)).toContain('session_id')
expect(indexNames(db)).toContain('idx_push_sub_session')
expect(db.prepare('SELECT endpoint, session_id FROM push_subscriptions').all()).toEqual([
{ endpoint: 'https://push.example.com/legacy', session_id: null },
])
expect(db.prepare('SELECT COUNT(*) AS count FROM applied_migrations WHERE id = ?').get(MIGRATION_ID))
.toEqual({ count: 1 })
db.close()
})
it('can run again on a database that always had the column', () => {
const db = new Database(':memory:')
migrate(db, allMigrations)
db.prepare('DELETE FROM applied_migrations WHERE id = ?').run(MIGRATION_ID)
migrate(db, allMigrations)
expect(columnNames(db).filter((name) => name === 'session_id')).toHaveLength(1)
expect(indexNames(db).filter((name) => name === 'idx_push_sub_session')).toHaveLength(1)
db.close()
})
})