mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
refactor(server): make the state ports async and route every reader through the provided store
The process-state ports were meant to let a store shared between processes replace the in-memory one, but they could not. The readers outside the container (the broadcast functions, the backup restore's permissions-cache flush, the pending-code sweep, hand-built consumers) went straight to the module-level in-memory instances, so an overridden provider was used by the gateway and the services while the broadcasts and the restore flush kept using the old store. And every port was synchronous, while a store in the database or on the network is async, so plugging one in would have changed every signature anyway. RateLimitStore, PendingCodeStore, OidcFlowStore and PermissionsCacheStore now return promises, and RateLimitService, OauthService, OidcService, PermissionsService and their callers await them. The in-memory stores still decide synchronously, so who gets a 429, a code or a cached level is unchanged. RoomRegistry stays synchronous: membership holds live sockets and broadcast() is a synchronous call every domain makes, so several processes need a fan-out in front of broadcast(), not a different place for the sets. The process-wide ports get a ProcessStoreSlot. PermissionsModule, OauthModule and RealtimeGatewayModule install the store the container resolved and release it on shutdown, and the free functions, the sweep and hand-built consumers read the slot, so overriding the provider changes every reader. Tests provide fake stores and check that the broadcasts, the restore flush and the sweep use them. Two callers needed more than an await. The transit MCP tools counted in a RateLimitService of their own that no provider could reach; they now take the one RateLimitModule provides (their mcp_transit_* buckets never overlap the REST ones). The link preview claimed its in-flight slot only after the budget check, which now yields, so simultaneous askers would each have been charged; it now claims the slot first, and an asker that joined someone out of budget asks again on its own budget. The unused putPendingCode/takePendingCode helpers are gone.
This commit is contained in:
@@ -1138,13 +1138,13 @@ describe('Tool: freeze_budget_rates', () => {
|
||||
|
||||
// budget_edit lowered to the owner, the way the admin permission panel does it.
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_budget_edit', 'trip_owner');
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
await withHarness(other.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'freeze_budget_rates', arguments: { tripId: trip.id } });
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
process.env.DEMO_MODE = 'true';
|
||||
const { user: demo } = createUser(testDb, { email: 'demo@nomad.app' });
|
||||
@@ -1160,7 +1160,7 @@ describe('Tool: freeze_budget_rates', () => {
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -235,7 +235,7 @@ describe('Tool: delete_day', () => {
|
||||
const day = createDay(testDb, trip.id);
|
||||
createDay(testDb, trip.id);
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_day_edit', 'trip_owner')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
try {
|
||||
await withHarness(member.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'delete_day', arguments: { tripId: trip.id, dayId: day.id } });
|
||||
@@ -244,7 +244,7 @@ describe('Tool: delete_day', () => {
|
||||
});
|
||||
} finally {
|
||||
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_day_edit'").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ interface FeedResult { feed_url: string | null }
|
||||
interface InviteLink { token: string; expires_at: string | null; created_at: string; url: string }
|
||||
interface InviteResult { invite_link: InviteLink | null }
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
// trip_invite_tokens is not in the shared helper's RESET_TABLES, and trip ids
|
||||
// restart after a DELETE-based reset, so a leftover row would attach itself
|
||||
@@ -71,7 +71,7 @@ beforeEach(() => {
|
||||
// The permission levels are cached in module state that resetTestDb knows
|
||||
// nothing about, so the one test that lowers share_manage would otherwise
|
||||
// keep the lowered level alive for every test after it.
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
delete process.env.DEMO_MODE;
|
||||
process.env.APP_URL = BASE;
|
||||
});
|
||||
|
||||
@@ -49,13 +49,13 @@ import { FilesMcp } from '../../../src/nest/files/files.mcp';
|
||||
import { FILE_CONTENT_MAX } from '../../../src/nest/files/files.service';
|
||||
import { StorageService } from '../../../src/nest/storage/storage.service';
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
broadcastMock.mockClear();
|
||||
delete process.env.DEMO_MODE;
|
||||
// resetTestDb truncates app_settings, but the permission cache is module-scoped
|
||||
// and would keep serving whatever the previous case configured.
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
@@ -68,9 +68,9 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>)
|
||||
}
|
||||
|
||||
/** Lower a configurable action the way the admin permission panel does. */
|
||||
function setPermission(action: string, level: string) {
|
||||
async function setPermission(action: string, level: string): Promise<void> {
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run(`perm_${action}`, level);
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
}
|
||||
|
||||
interface FileRowOverrides {
|
||||
@@ -435,7 +435,7 @@ describe('Tool: update_trip_file', () => {
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
addTripMember(testDb, trip.id, member.id);
|
||||
const file = insertFile(trip.id);
|
||||
setPermission('file_edit', 'trip_owner');
|
||||
await setPermission('file_edit', 'trip_owner');
|
||||
|
||||
await withHarness(member.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
@@ -574,7 +574,7 @@ describe('Tool: upload_trip_file', () => {
|
||||
const { user: member } = createUser(testDb);
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
addTripMember(testDb, trip.id, member.id);
|
||||
setPermission('file_upload', 'trip_owner');
|
||||
await setPermission('file_upload', 'trip_owner');
|
||||
await withHarness(member.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'upload_trip_file', arguments: { tripId: trip.id, filename: 'a.pdf', content: pdf } });
|
||||
expect(result.isError).toBe(true);
|
||||
@@ -708,7 +708,7 @@ describe('Tool: link_trip_file', () => {
|
||||
addTripMember(testDb, trip.id, member.id);
|
||||
const reservation = createReservation(testDb, trip.id);
|
||||
const file = insertFile(trip.id);
|
||||
setPermission('file_edit', 'trip_owner');
|
||||
await setPermission('file_edit', 'trip_owner');
|
||||
|
||||
await withHarness(member.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
@@ -811,7 +811,7 @@ describe('Tool: unlink_trip_file', () => {
|
||||
const file = insertFile(trip.id);
|
||||
testDb.prepare('INSERT INTO file_links (file_id, reservation_id) VALUES (?, ?)').run(file.id, reservation.id);
|
||||
const linkId = linkRows(file.id)[0].id;
|
||||
setPermission('file_edit', 'trip_owner');
|
||||
await setPermission('file_edit', 'trip_owner');
|
||||
|
||||
await withHarness(member.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
|
||||
@@ -106,7 +106,7 @@ const itinerary = {
|
||||
],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
geocodeMock.mockReset();
|
||||
planMock.mockReset();
|
||||
@@ -114,7 +114,7 @@ beforeEach(() => {
|
||||
// mockReset would fall back to the real notification write — keep it stubbed.
|
||||
notifyBookingChangeMock.mockReset().mockImplementation(async () => {});
|
||||
delete process.env.DEMO_MODE;
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
});
|
||||
|
||||
afterAll(() => testDb.close());
|
||||
|
||||
@@ -26,13 +26,13 @@ import { invalidatePermissionsCache } from '../../../src/nest/permissions/permis
|
||||
import { createUser, createAdmin, createTrip, addTripMember } from '../../helpers/factories';
|
||||
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
broadcastMock.mockClear();
|
||||
delete process.env.DEMO_MODE;
|
||||
// resetTestDb truncates app_settings, but the permission cache is module-scoped
|
||||
// and would keep serving whatever the previous case configured.
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
@@ -45,9 +45,9 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>)
|
||||
}
|
||||
|
||||
/** Lower a configurable action the way the admin permission panel does. */
|
||||
function setPermission(action: string, level: string) {
|
||||
async function setPermission(action: string, level: string): Promise<void> {
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run(`perm_${action}`, level);
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -157,7 +157,7 @@ describe('Tool: add_trip_member', () => {
|
||||
const { user: outsider } = createUser(testDb);
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
addTripMember(testDb, trip.id, collaborator.id);
|
||||
setPermission('member_manage', 'trip_member');
|
||||
await setPermission('member_manage', 'trip_member');
|
||||
await withHarness(collaborator.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
name: 'add_trip_member',
|
||||
@@ -274,7 +274,7 @@ describe('Tool: remove_trip_member', () => {
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
addTripMember(testDb, trip.id, member.id);
|
||||
addTripMember(testDb, trip.id, other.id);
|
||||
setPermission('member_manage', 'trip_member');
|
||||
await setPermission('member_manage', 'trip_member');
|
||||
await withHarness(member.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
name: 'remove_trip_member',
|
||||
@@ -867,7 +867,7 @@ describe('Guest tools stay owner-only', () => {
|
||||
let guestId = 0;
|
||||
await withHarness(owner.id, async (h) => { guestId = await makeGuest(h, trip.id, 'Anna'); });
|
||||
|
||||
setPermission('member_manage', 'trip_member');
|
||||
await setPermission('member_manage', 'trip_member');
|
||||
await withHarness(collaborator.id, async (h) => {
|
||||
expect((await h.client.callTool({
|
||||
name: 'create_trip_guest', arguments: { tripId: trip.id, name: 'Bea' },
|
||||
|
||||
Reference in New Issue
Block a user