fix(realtime): trip, member, chat, file, settings, tag and notification writes in parity

This commit is contained in:
Maurice
2026-10-22 12:14:27 +02:00
parent 94b8c3194c
commit ce7b5bf453
51 changed files with 2055 additions and 382 deletions
@@ -1,4 +1,5 @@
import type { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
import type { SettingsWritesService } from '../../../src/nest/settings/settings-writes.service';
import { SettingsController } from '../../../src/nest/settings/settings.controller';
import type { SettingsService } from '../../../src/nest/settings/settings.service';
import type { User } from '../../../src/types';
@@ -30,12 +31,34 @@ async function thrown(fn: () => unknown): Promise<{ status: number; body: unknow
beforeEach(() => vi.clearAllMocks());
/**
* SettingsWritesService over the stubbed service: the writes go through it, so
* it hands each on and records the socket the route read. The settings:updated
* it sends is held by tools-settings.test.ts and the broadcast parity suite.
*/
function writesFor(s: SettingsService) {
return {
saveOne: vi.fn((userId: number, key: string, value: unknown, _socketId: string | undefined) =>
s.upsertSetting(userId, key, value),
),
saveMany: vi.fn((userId: number, values: Record<string, unknown>, _socketId: string | undefined) =>
s.bulkUpsertSettings(userId, values),
),
};
}
let writes: ReturnType<typeof writesFor>;
function controller(s: SettingsService) {
writes = writesFor(s);
const env = { isManaged: () => false } as unknown as RuntimeEnvService;
return new SettingsController(s, env, writes as unknown as SettingsWritesService);
}
describe('SettingsController', () => {
it('GET / returns the settings', async () => {
expect(
await new SettingsController(
await controller(
svc({ getUserSettings: vi.fn().mockReturnValue({ theme: 'dark' }) } as Partial<SettingsService>),
{ isManaged: () => false } as unknown as RuntimeEnvService,
).list(user),
).toEqual({ settings: { theme: 'dark' } });
});
@@ -45,28 +68,27 @@ describe('SettingsController', () => {
it('PUT / no-ops on the masked sentinel without writing', async () => {
const upsertSetting = vi.fn();
const c = new SettingsController(svc({ upsertSetting } as Partial<SettingsService>), {
isManaged: () => false,
} as unknown as RuntimeEnvService);
const c = controller(svc({ upsertSetting } as Partial<SettingsService>));
expect(await c.upsert(user, { key: 'immich_api_key', value: '••••••••' })).toEqual({
success: true,
key: 'immich_api_key',
unchanged: true,
});
expect(upsertSetting).not.toHaveBeenCalled();
expect(writes.saveOne).not.toHaveBeenCalled();
});
it('PUT / writes a real value', async () => {
const upsertSetting = vi.fn();
const c = new SettingsController(svc({ upsertSetting } as Partial<SettingsService>), {
isManaged: () => false,
} as unknown as RuntimeEnvService);
expect(await c.upsert(user, { key: 'theme', value: 'dark' })).toEqual({
const c = controller(svc({ upsertSetting } as Partial<SettingsService>));
expect(await c.upsert(user, { key: 'theme', value: 'dark' }, 'sock')).toEqual({
success: true,
key: 'theme',
value: 'dark',
});
expect(upsertSetting).toHaveBeenCalledWith(1, 'theme', 'dark');
// Through the writes, which tell the user's other tabs and leave out this one.
expect(writes.saveOne).toHaveBeenCalledWith(1, 'theme', 'dark', 'sock');
});
it('POST /bulk 500 on a write error, else returns the count', async () => {
@@ -75,21 +97,22 @@ describe('SettingsController', () => {
vi.spyOn(console, 'error').mockImplementation(() => {});
expect(
await thrown(() =>
new SettingsController(
controller(
svc({
bulkUpsertSettings: vi.fn(() => {
throw new Error('db');
}),
} as Partial<SettingsService>),
{ isManaged: () => false } as unknown as RuntimeEnvService,
).bulk(user, { settings: { a: 1 } }),
),
).toEqual({ status: 500, body: { error: 'Error saving settings' } });
expect(
await new SettingsController(
svc({ bulkUpsertSettings: vi.fn().mockReturnValue(3) } as Partial<SettingsService>),
{ isManaged: () => false } as unknown as RuntimeEnvService,
).bulk(user, { settings: { a: 1, b: 2, c: 3 } }),
await controller(svc({ bulkUpsertSettings: vi.fn().mockReturnValue(3) } as Partial<SettingsService>)).bulk(
user,
{ settings: { a: 1, b: 2, c: 3 } },
'sock',
),
).toEqual({ success: true, updated: 3 });
expect(writes.saveMany).toHaveBeenCalledWith(1, { a: 1, b: 2, c: 3 }, 'sock');
});
});