import { test } from 'node:test'; import assert from 'node:assert/strict'; import { randomUUID } from 'node:crypto'; import { eq } from 'drizzle-orm'; import { fixture } from './helpers.js'; import * as s from '../src/db/schema.js'; test('주변 발견은 방 참여자를 제외하고 한 기기는 한 방만 참여, 방 종료 후 모두 주변으로 복귀', async () => { const f = await fixture(true); try { const a = await f.create('8.8.8.8'), b = await f.create('8.8.8.8'), c = await f.create('8.8.8.8'); assert.equal((await f.call(a, '/discovery/devices')).json().devices.length, 2); const pending = (await f.request(a, b)).json().requestId; const room = (await f.call(a, '/spaces', 'POST', {})).json(); assert.equal( f.service.db.select().from(s.requests).where(eq(s.requests.id, pending)).get().status, 'cancelled', ); assert.equal((await f.call(a, '/discovery/devices')).json().devices.length, 0); assert.equal( (await f.call(b, '/discovery/devices')) .json() .devices.some((device) => device.id === a.deviceId), false, ); assert.equal((await f.request(b, a)).statusCode, 403); assert.equal((await f.call(b, '/spaces/join', 'POST', { code: room.code })).statusCode, 200); assert.equal((await f.call(b, '/discovery/devices')).json().devices.length, 1); assert.equal((await f.call(b, '/spaces', 'POST', {})).statusCode, 409); const other = (await f.call(c, '/spaces', 'POST', {})).json(); assert.equal((await f.call(b, '/spaces/join', 'POST', { code: other.code })).statusCode, 409); assert.throws( () => f.service.db .insert(s.members) .values({ id: randomUUID(), spaceId: other.space.id, deviceId: b.deviceId, status: 'approved', joinedAt: Date.now(), }) .run(), /UNIQUE/, ); assert.equal((await f.call(a, `/spaces/${room.space.id}`, 'DELETE')).statusCode, 200); assert.equal((await f.call(b, '/spaces')).json().spaces.length, 0); assert.equal((await f.call(a, '/discovery/devices')).json().devices.length, 1); await f.call(c, `/spaces/${other.space.id}`, 'DELETE'); assert.equal((await f.call(a, '/discovery/devices')).json().devices.length, 2); assert.equal((await f.call(b, '/spaces', 'POST', {})).statusCode, 200); } finally { await f.close(); } }); test('승인 대기 참여자도 주변에서 숨고 방장이 승인한 뒤에만 같은 방 전송 가능', async () => { const f = await fixture(true); try { const a = await f.create('8.8.8.8'), b = await f.create('8.8.8.8'), c = await f.create('8.8.8.8'); const room = await f.space(a, b, true); assert.equal((await f.call(c, '/discovery/devices')).json().devices.length, 0); assert.equal((await f.request(a, b)).statusCode, 403); await f.call(a, `/spaces/${room.space.id}/members/${b.deviceId}/approve`, 'POST', {}); const request = await f.request(a, b); assert.equal(request.statusCode, 200); await f.call( b, `/transfer-requests/${request.json().requestId}/accept`, 'POST', {}, randomUUID(), ); await f.call(b, `/spaces/${room.space.id}/members/${b.deviceId}`, 'DELETE'); assert.equal( f.service.db .select() .from(s.requests) .where(eq(s.requests.id, request.json().requestId)) .get().status, 'cancelled', ); } finally { await f.close(); } });