mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
96 lines
3.5 KiB
TypeScript
96 lines
3.5 KiB
TypeScript
import type { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
|
import { SettingsController } from '../../../src/nest/settings/settings.controller';
|
|
import type { SettingsService } from '../../../src/nest/settings/settings.service';
|
|
import type { User } from '../../../src/types';
|
|
import { HttpException } from '@nestjs/common';
|
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
const user = { id: 1, role: 'user', email: 'u@example.test' } as User;
|
|
|
|
function svc(o: Partial<SettingsService> = {}): SettingsService {
|
|
return {
|
|
getUserSettings: vi.fn(),
|
|
upsertSetting: vi.fn(),
|
|
bulkUpsertSettings: vi.fn(),
|
|
...o,
|
|
} as unknown as SettingsService;
|
|
}
|
|
|
|
async function thrown(fn: () => unknown): Promise<{ status: number; body: unknown }> {
|
|
try {
|
|
await fn();
|
|
} catch (err) {
|
|
expect(err).toBeInstanceOf(HttpException);
|
|
const e = err as HttpException;
|
|
return { status: e.getStatus(), body: e.getResponse() };
|
|
}
|
|
throw new Error('expected throw');
|
|
}
|
|
|
|
beforeEach(() => vi.clearAllMocks());
|
|
|
|
describe('SettingsController', () => {
|
|
it('GET / returns the settings', async () => {
|
|
expect(
|
|
await new SettingsController(
|
|
svc({ getUserSettings: vi.fn().mockReturnValue({ theme: 'dark' }) } as Partial<SettingsService>),
|
|
{ isManaged: () => false } as unknown as RuntimeEnvService,
|
|
).list(user),
|
|
).toEqual({ settings: { theme: 'dark' } });
|
|
});
|
|
|
|
// The 400-without-a-key path moved to the global ZodValidationPipe (via
|
|
// SettingUpsertDto) — pinned by the settings e2e, not reachable here.
|
|
|
|
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);
|
|
expect(await c.upsert(user, { key: 'immich_api_key', value: '••••••••' })).toEqual({
|
|
success: true,
|
|
key: 'immich_api_key',
|
|
unchanged: true,
|
|
});
|
|
expect(upsertSetting).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({
|
|
success: true,
|
|
key: 'theme',
|
|
value: 'dark',
|
|
});
|
|
expect(upsertSetting).toHaveBeenCalledWith(1, 'theme', 'dark');
|
|
});
|
|
|
|
it('POST /bulk 500 on a write error, else returns the count', async () => {
|
|
// The 400-without-an-object path moved to the global ZodValidationPipe
|
|
// (via SettingsBulkDto) — pinned by the settings e2e, not reachable here.
|
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
expect(
|
|
await thrown(() =>
|
|
new SettingsController(
|
|
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 } }),
|
|
).toEqual({ success: true, updated: 3 });
|
|
});
|
|
});
|