38 lines
2.1 KiB
TypeScript
38 lines
2.1 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { MongoClient } from 'mongodb';
|
|
import { MongoVisitStore } from '../server/visits.ts';
|
|
import { loadServerConfig } from '../server/config.ts';
|
|
|
|
// Isolated disposable database: never uses the application's mongoDatabase.
|
|
const database = `sunrise_verify_${randomUUID().replaceAll('-', '')}`;
|
|
const uri = loadServerConfig().mongoUri;
|
|
const reader = new MongoClient(uri, { serverSelectionTimeoutMS: 5000 });
|
|
let store: MongoVisitStore | undefined;
|
|
try {
|
|
store = await MongoVisitStore.connect(uri, database);
|
|
const id = randomUUID(), now = new Date();
|
|
await store.insert({ _id: id, visitorId: randomUUID(), nickname: '연결 검증', locationId: 'gwanaksan',
|
|
roomId: 'gwanaksan-001', joinedAt: now, lastSeenAt: now, leftAt: null, endReason: null, serverInstanceId: randomUUID() });
|
|
await store.heartbeat([id], new Date(+now + 1000));
|
|
// A second connection must not rewrite an existing open visit.
|
|
const second = await MongoVisitStore.connect(uri, database);
|
|
await second.close();
|
|
await reader.connect();
|
|
const open = await reader.db(database).collection<{ _id: string; endReason: string | null }>('visitSessions').findOne({ _id: id });
|
|
assert.equal(open?.endReason, null, 'startup must not rewrite existing open visits');
|
|
await store.finish(id, new Date(+now + 2000), 'disconnect');
|
|
await reader.connect();
|
|
const record = await reader.db(database).collection<{ _id: string; lastSeenAt: Date; leftAt: Date; endReason: string }>('visitSessions').findOne({ _id: id });
|
|
assert.equal(record?.endReason, 'disconnect');
|
|
assert.equal(record?.leftAt.getTime(), +now + 2000);
|
|
assert.equal(record?.lastSeenAt.getTime(), +now + 2000);
|
|
assert.ok(!('equipment' in record!) && !('x' in record!), 'no transient player state in visit log');
|
|
console.log(JSON.stringify({ ok: true, tested: ['insert', 'heartbeat', 'finish', 'read'], isolatedDatabase: database }));
|
|
} finally {
|
|
await store?.close();
|
|
// This exact random database was created by this verification run alone.
|
|
if (store) await reader.db(database).dropDatabase();
|
|
await reader.close();
|
|
}
|