From 6f4c655e62f32ffe4e27f9a16184ff8b31d4159a Mon Sep 17 00:00:00 2001 From: OpenVelo Agent Date: Wed, 22 Jul 2026 14:09:01 +0000 Subject: [PATCH 1/2] docs: update documentation to OKF v0.1 format --- docs/api/admin.md | 8 ++-- docs/architecture/key-files.md | 3 ++ docs/guides/admin-general-settings.md | 65 ++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/docs/api/admin.md b/docs/api/admin.md index 445e7ce..93d388c 100644 --- a/docs/api/admin.md +++ b/docs/api/admin.md @@ -46,9 +46,11 @@ removes the row. * `logo` up to 2048 chars (a public URL — uploaded separately) * `welcomeMarkdown` up to 64 000 chars * `themeKey` is one of `THEME_IDS` -* `eventStartUtc` / `eventEndUtc` parseable dates; `superRefine` - enforces `eventEndUtc > eventStartUtc` and reports the issue on - `eventEndUtc` +* `eventStartUtc` / `eventEndUtc` are validated as ISO-8601 datetimes + (zod `string().datetime(...)`) — empty strings are explicitly allowed + to keep the event window unconfigured. `superRefine` then enforces + `eventEndUtc > eventStartUtc` and reports the issue on `eventEndUtc` + when both fields are non-empty. * `defaultChallengeIp` 1–255 chars * `registrationsEnabled` boolean diff --git a/docs/architecture/key-files.md b/docs/architecture/key-files.md index 283b5da..8f7484f 100644 --- a/docs/architecture/key-files.md +++ b/docs/architecture/key-files.md @@ -25,6 +25,7 @@ timestamp: 2026-07-22T13:40:00Z | `backend/src/modules/auth/auth.controller.ts` | Registers authentication and account endpoints. | | `backend/src/modules/auth/auth.service.ts` | Handles sessions, authentication, registration, and password changes. | | `backend/src/modules/uploads/uploads.controller.ts` | Registers admin-only multipart uploads, including Sharp-backed site-logo format validation. | +| `backend/src/modules/admin/dto/general.dto.ts` | Zod schema for `PUT /api/v1/admin/general/settings` — string-length rules, theme-key enum, ISO-8601 datetime check on the event-window fields (empty string allowed), and the `endAfterStart` super-refine. | # Frontend @@ -45,6 +46,8 @@ timestamp: 2026-07-22T13:40:00Z | `frontend/src/app/features/shell/header/shell-header.component.ts` | Shell title, status LED (size/shape only — color comes from the pure map via `[style.background-color]`), countdown, and user menu. | | `frontend/src/app/features/shell/tabs/quick-tabs.component.ts` | Main shell navigation tabs. | | `frontend/src/app/features/shell/change-password/change-password-modal.component.ts` | Change-password form modal. | +| `frontend/src/app/features/admin/general.component.ts` | `AdminGeneralComponent` reactive form for `/admin/general` — per-field inline error rendering (page-title + event-start + event-end), logo upload wiring, welcome Markdown preview, event-state derivation, and SSE `general` event handling. | +| `frontend/src/app/features/admin/general.pure.ts` | Pure helpers used by `AdminGeneralComponent` — `deriveEventState`, `endAfterStartValidator`, `normalizePageTitle`, `toIsoUtc`, `toDatetimeLocal`, `pageTitleError` / `pageTitleMessage`, `isoDatetimeValidator`, and `datetimeMessage`. | | `tests/frontend/authenticated-event-source.spec.ts` | Tests SSE authorization, frame transport behavior, and the `401`/`403` unauthorized path. | | `tests/frontend/auth-session-events.spec.ts` | Pure tests for cross-tab invalidation message encoding, payload validation, and `storage`-event filtering. | diff --git a/docs/guides/admin-general-settings.md b/docs/guides/admin-general-settings.md index d2a6ccb..39cdc30 100644 --- a/docs/guides/admin-general-settings.md +++ b/docs/guides/admin-general-settings.md @@ -2,8 +2,8 @@ type: guide title: Admin — General Settings description: How an admin edits global platform settings (page title, logo, theme, event window, default challenge IP, registrations, welcome Markdown) from the /admin/general page. -tags: [guide, admin, settings, general, tester] -timestamp: 2026-07-22T12:44:45Z +tags: [guide, admin, settings, general, tester, datetime, validation] +timestamp: 2026-07-22T14:07:58Z --- # When this view is available @@ -42,8 +42,8 @@ The page is a single reactive form with these controls (every | Logo (file picker) | `general-logo-file` | `logo` (public URL) | Uploads via `POST /api/v1/uploads/logo`; the returned `publicUrl` is bound to a hidden input `general-logo`. | | Logo upload status | `general-logo-uploading` / `general-logo-error` / `general-logo-current` | — | Inline status text under the file picker. | | Global theme | `general-themeKey` | `themeKey` | ` + + @if (showEventStartError()) { +
{{ eventStartMessage() }}
+ }
- + + @if (showEventEndError()) { +
{{ eventEndMessage() }}
+ } @if (form.controls.eventEndUtc.touched && form.error?.['endBeforeStart']) { -
Event end must be after event start.
+
Event end must be after event start.
}
@@ -179,14 +201,22 @@ export class AdminGeneralComponent implements OnInit { private readonly pageTitleInvalid = signal(false); private readonly pageTitleTouchedOrDirty = signal(false); + private readonly eventStartValue = signal(''); + private readonly eventStartInvalid = signal(false); + private readonly eventStartTouchedOrDirty = signal(false); + + private readonly eventEndValue = signal(''); + private readonly eventEndInvalid = signal(false); + private readonly eventEndTouchedOrDirty = signal(false); + readonly form = this.fb.nonNullable.group( { pageTitle: this.fb.nonNullable.control('', [Validators.required, Validators.maxLength(120), this.pageTitleNotBlankValidator()]), logo: this.fb.nonNullable.control(''), welcomeMarkdown: this.fb.nonNullable.control(''), themeKey: this.fb.nonNullable.control('classic'), - eventStartUtc: this.fb.nonNullable.control(''), - eventEndUtc: this.fb.nonNullable.control(''), + eventStartUtc: this.fb.nonNullable.control('', [isoDatetimeValidator]), + eventEndUtc: this.fb.nonNullable.control('', [isoDatetimeValidator]), defaultChallengeIp: this.fb.nonNullable.control('', [Validators.required]), registrationsEnabled: this.fb.nonNullable.control(false), }, @@ -210,6 +240,22 @@ export class AdminGeneralComponent implements OnInit { pageTitleMessage(this.pageTitleValue(), this.form.controls.pageTitle.errors), ); + readonly showEventStartError = computed( + () => this.eventStartTouchedOrDirty() && this.eventStartInvalid(), + ); + + readonly eventStartMessage = computed(() => + datetimeMessage('Event start', this.eventStartValue(), this.form.controls.eventStartUtc.errors), + ); + + readonly showEventEndError = computed( + () => this.eventEndTouchedOrDirty() && this.eventEndInvalid(), + ); + + readonly eventEndMessage = computed(() => + datetimeMessage('Event end', this.eventEndValue(), this.form.controls.eventEndUtc.errors), + ); + constructor() { this.form.controls.welcomeMarkdown.valueChanges .pipe(takeUntilDestroyed(this.destroyRef)) @@ -231,6 +277,40 @@ export class AdminGeneralComponent implements OnInit { this.pageTitleInvalid.set(pt.invalid); this.pageTitleTouchedOrDirty.set(pt.touched || pt.dirty); }); + + const es = this.form.controls.eventStartUtc; + this.eventStartValue.set(es.value); + this.eventStartInvalid.set(es.invalid); + this.eventStartTouchedOrDirty.set(es.touched || es.dirty); + es.valueChanges + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe((v) => { + this.eventStartValue.set(v); + this.eventStartTouchedOrDirty.set(es.touched || es.dirty); + }); + es.statusChanges + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe(() => { + this.eventStartInvalid.set(es.invalid); + this.eventStartTouchedOrDirty.set(es.touched || es.dirty); + }); + + const ee = this.form.controls.eventEndUtc; + this.eventEndValue.set(ee.value); + this.eventEndInvalid.set(ee.invalid); + this.eventEndTouchedOrDirty.set(ee.touched || ee.dirty); + ee.valueChanges + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe((v) => { + this.eventEndValue.set(v); + this.eventEndTouchedOrDirty.set(ee.touched || ee.dirty); + }); + ee.statusChanges + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe(() => { + this.eventEndInvalid.set(ee.invalid); + this.eventEndTouchedOrDirty.set(ee.touched || ee.dirty); + }); } async ngOnInit(): Promise { @@ -272,6 +352,14 @@ export class AdminGeneralComponent implements OnInit { this.form.controls.pageTitle.markAsUntouched(); this.form.controls.pageTitle.markAsPristine(); this.pageTitleTouchedOrDirty.set(false); + + this.form.controls.eventStartUtc.markAsUntouched(); + this.form.controls.eventStartUtc.markAsPristine(); + this.eventStartTouchedOrDirty.set(false); + + this.form.controls.eventEndUtc.markAsUntouched(); + this.form.controls.eventEndUtc.markAsPristine(); + this.eventEndTouchedOrDirty.set(false); } async onSubmit(): Promise { diff --git a/frontend/src/app/features/admin/general.pure.ts b/frontend/src/app/features/admin/general.pure.ts index 6a44b06..0db8f5d 100644 --- a/frontend/src/app/features/admin/general.pure.ts +++ b/frontend/src/app/features/admin/general.pure.ts @@ -64,3 +64,25 @@ export function toIsoUtc(local: string): string { if (Number.isNaN(d.getTime())) return local; return d.toISOString(); } + +export function isoDatetimeValidator(ctrl: { value: string | null | undefined }): { invalidDatetime: true } | null { + const v = ctrl?.value ?? ''; + if (v === '') return null; + const t = Date.parse(v); + return Number.isFinite(t) ? null : { invalidDatetime: true }; +} + +export function datetimeMessage( + fieldLabel: string, + value: string | null | undefined, + controlErrors: { [key: string]: unknown } | null | undefined, +): string | null { + if (controlErrors?.['invalidDatetime']) { + return `${fieldLabel} must be a valid ISO-8601 datetime.`; + } + const v = value ?? ''; + if (v !== '' && !Number.isFinite(Date.parse(v))) { + return `${fieldLabel} must be a valid ISO-8601 datetime.`; + } + return null; +} diff --git a/tests/backend/admin-general-service.spec.ts b/tests/backend/admin-general-service.spec.ts index 8f37606..a54b40c 100644 --- a/tests/backend/admin-general-service.spec.ts +++ b/tests/backend/admin-general-service.spec.ts @@ -87,6 +87,73 @@ describe('GeneralSettingsSchema - validation rules', () => { expect(r.data.pageTitle).toBe('OpenVelo'); } }); + + it('rejects a non-datetime eventStartUtc ("not-a-date")', () => { + const r = GeneralSettingsSchema.safeParse({ + pageTitle: 'T', + logo: '', + welcomeMarkdown: '', + themeKey: 'classic', + eventStartUtc: 'not-a-date', + eventEndUtc: '2026-02-01T00:00:00Z', + defaultChallengeIp: '127.0.0.1', + registrationsEnabled: false, + }); + expect(r.success).toBe(false); + if (!r.success) { + const paths = r.error.issues.map((i) => i.path.join('.')); + expect(paths).toContain('eventStartUtc'); + expect(r.error.issues.some((i) => /datetime/i.test(i.message))).toBe(true); + } + }); + + it('rejects a non-datetime eventEndUtc and reports the issue on eventEndUtc', () => { + const r = GeneralSettingsSchema.safeParse({ + pageTitle: 'T', + logo: '', + welcomeMarkdown: '', + themeKey: 'classic', + eventStartUtc: '2026-08-15T10:30:00.000Z', + eventEndUtc: 'also-not-a-date', + defaultChallengeIp: '127.0.0.1', + registrationsEnabled: false, + }); + expect(r.success).toBe(false); + if (!r.success) { + const paths = r.error.issues.map((i) => i.path.join('.')); + expect(paths).toContain('eventEndUtc'); + expect(paths).not.toContain('eventStartUtc'); + expect(r.error.issues.some((i) => /datetime/i.test(i.message))).toBe(true); + } + }); + + it('rejects both fields as non-datetime strings (the Job negative case)', () => { + const r = GeneralSettingsSchema.safeParse({ + pageTitle: 'T', + logo: '', + welcomeMarkdown: '', + themeKey: 'classic', + eventStartUtc: 'not-a-date', + eventEndUtc: 'also-not-a-date', + defaultChallengeIp: '127.0.0.1', + registrationsEnabled: false, + }); + expect(r.success).toBe(false); + }); + + it('accepts empty strings (unconfigured event window) for both datetime fields', () => { + const r = GeneralSettingsSchema.safeParse({ + pageTitle: 'T', + logo: '', + welcomeMarkdown: '', + themeKey: 'classic', + eventStartUtc: '', + eventEndUtc: '', + defaultChallengeIp: '127.0.0.1', + registrationsEnabled: false, + }); + expect(r.success).toBe(true); + }); }); describe('AdminGeneralService.updateSettings - happy path', () => { diff --git a/tests/frontend/admin-general-pure.spec.ts b/tests/frontend/admin-general-pure.spec.ts index e5c5df2..60b9c27 100644 --- a/tests/frontend/admin-general-pure.spec.ts +++ b/tests/frontend/admin-general-pure.spec.ts @@ -1,6 +1,8 @@ import { + datetimeMessage, deriveEventState, endAfterStartValidator, + isoDatetimeValidator, normalizePageTitle, pageTitleError, pageTitleMessage, @@ -161,3 +163,43 @@ describe('pageTitleMessage', () => { expect(pageTitleMessage('a'.repeat(121), { maxlength: { requiredLength: 120 } })).toBe(maxLengthMsg); }); }); + +describe('isoDatetimeValidator', () => { + it('returns null for an empty value (unconfigured is allowed)', () => { + expect(isoDatetimeValidator({ value: '' })).toBeNull(); + expect(isoDatetimeValidator({ value: null })).toBeNull(); + expect(isoDatetimeValidator({ value: undefined })).toBeNull(); + }); + + it('returns null for a valid ISO-8601 datetime', () => { + expect(isoDatetimeValidator({ value: '2026-08-15T10:30:00.000Z' })).toBeNull(); + }); + + it('returns { invalidDatetime: true } for a non-datetime string', () => { + expect(isoDatetimeValidator({ value: 'not-a-date' })).toEqual({ invalidDatetime: true }); + expect(isoDatetimeValidator({ value: 'also-not-a-date' })).toEqual({ invalidDatetime: true }); + }); +}); + +describe('datetimeMessage', () => { + it('returns the per-field error when controlErrors carries invalidDatetime', () => { + expect(datetimeMessage('Event start', 'not-a-date', { invalidDatetime: true })) + .toBe('Event start must be a valid ISO-8601 datetime.'); + expect(datetimeMessage('Event end', 'garbage', { invalidDatetime: true })) + .toBe('Event end must be a valid ISO-8601 datetime.'); + }); + + it('falls back to detecting an unparseable value when controlErrors are absent', () => { + expect(datetimeMessage('Event start', 'not-a-date', null)) + .toBe('Event start must be a valid ISO-8601 datetime.'); + }); + + it('returns null for an empty value', () => { + expect(datetimeMessage('Event start', '', null)).toBeNull(); + expect(datetimeMessage('Event start', '', { invalidDatetime: false as any })).toBeNull(); + }); + + it('returns null for a valid ISO-8601 datetime', () => { + expect(datetimeMessage('Event start', '2026-08-15T10:30:00.000Z', null)).toBeNull(); + }); +}); -- 2.52.0