f2457113a2
Die Middleware liest jetzt zusaetzlich dv/dc/dos aus der Anfrage und legt daraus das Cookie tessera_desktop_client an (bereinigt per Muster, nur wenn alle drei Werte gueltig sind); desktop-client.ts liest es zurueck. Der Fehler-melden-Dialog fuellt daraus vier neue Nutzlastfelder (clientKind/clientOs/clientVersion/clientCommit), damit die API die Herkunft der Meldung ausweisen kann. Ohne das zweite Cookie (alter Client) bleibt es bei "Desktop-App (unbekannt)". Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016g2npLxzH5gZpg8s2S6vKh
110 lines
3.7 KiB
TypeScript
110 lines
3.7 KiB
TypeScript
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import {
|
|
DESKTOP_CLIENT_COOKIE_NAME,
|
|
DESKTOP_COOKIE_NAME,
|
|
getDesktopClientInfo,
|
|
isDesktopClient,
|
|
parseDesktopClientCookie,
|
|
useIsDesktopClient,
|
|
} from './desktop-client';
|
|
|
|
/**
|
|
* desktop-client.test — Gegenstueck zur Middleware (260917-h2s): liest das
|
|
* Cookie `tessera_desktop`, das die Middleware auf jede Antwort setzt, wenn
|
|
* der Desktop-Client `?desktop=1` an seine erste Navigation anhaengt.
|
|
* Seit quick-260918-gza zusaetzlich `parseDesktopClientCookie()` /
|
|
* `getDesktopClientInfo()` fuer das zweite Cookie `tessera_desktop_client`.
|
|
*/
|
|
function clearCookie() {
|
|
document.cookie = `${DESKTOP_COOKIE_NAME}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
|
|
document.cookie = `${DESKTOP_CLIENT_COOKIE_NAME}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
|
|
}
|
|
|
|
afterEach(() => {
|
|
clearCookie();
|
|
});
|
|
|
|
describe('isDesktopClient', () => {
|
|
it('ohne Cookie: false', () => {
|
|
expect(isDesktopClient()).toBe(false);
|
|
});
|
|
|
|
it('mit tessera_desktop=1: true', () => {
|
|
document.cookie = `${DESKTOP_COOKIE_NAME}=1; path=/`;
|
|
expect(isDesktopClient()).toBe(true);
|
|
});
|
|
|
|
it('mit tessera_desktop=0: false', () => {
|
|
document.cookie = `${DESKTOP_COOKIE_NAME}=0; path=/`;
|
|
expect(isDesktopClient()).toBe(false);
|
|
});
|
|
|
|
it('ohne document: false', () => {
|
|
vi.stubGlobal('document', undefined);
|
|
expect(isDesktopClient()).toBe(false);
|
|
vi.unstubAllGlobals();
|
|
});
|
|
});
|
|
|
|
describe('useIsDesktopClient', () => {
|
|
it('Cookie gesetzt: liefert nach dem Effekt true', async () => {
|
|
document.cookie = `${DESKTOP_COOKIE_NAME}=1; path=/`;
|
|
const { result } = renderHook(() => useIsDesktopClient());
|
|
await waitFor(() => expect(result.current).toBe(true));
|
|
});
|
|
|
|
it('ohne Cookie: bleibt false', async () => {
|
|
const { result } = renderHook(() => useIsDesktopClient());
|
|
await act(async () => {});
|
|
expect(result.current).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('getDesktopClientInfo / parseDesktopClientCookie (quick-260918-gza)', () => {
|
|
it('kodierte Form (wie der Browser sie in document.cookie haelt)', () => {
|
|
expect(
|
|
parseDesktopClientCookie('tessera_desktop=1; tessera_desktop_client=1.2.0%7Ca6d1a64%7Cwindows'),
|
|
).toEqual({ version: '1.2.0', commit: 'a6d1a64', os: 'windows' });
|
|
});
|
|
|
|
it('rohe Form (nicht kodiert)', () => {
|
|
expect(parseDesktopClientCookie('tessera_desktop_client=1.2.0|a6d1a64|windows')).toEqual({
|
|
version: '1.2.0',
|
|
commit: 'a6d1a64',
|
|
os: 'windows',
|
|
});
|
|
});
|
|
|
|
it('leerer Commit bleibt leer', () => {
|
|
expect(parseDesktopClientCookie('tessera_desktop_client=1.2.0%7C%7Clinux')).toEqual({
|
|
version: '1.2.0',
|
|
commit: '',
|
|
os: 'linux',
|
|
});
|
|
});
|
|
|
|
it('ohne Cookie: null', () => {
|
|
expect(parseDesktopClientCookie('tessera_desktop=1')).toBeNull();
|
|
});
|
|
|
|
it('unvollstaendige oder leere Pflichtfelder: null', () => {
|
|
expect(parseDesktopClientCookie('tessera_desktop_client=abc')).toBeNull();
|
|
expect(parseDesktopClientCookie('tessera_desktop_client=1.2.0|x')).toBeNull();
|
|
expect(parseDesktopClientCookie('tessera_desktop_client=|a6d1a64|linux')).toBeNull();
|
|
expect(parseDesktopClientCookie('tessera_desktop_client=1.2.0|a6d1a64|')).toBeNull();
|
|
});
|
|
|
|
it('getDesktopClientInfo() liest document.cookie', () => {
|
|
expect(getDesktopClientInfo()).toBeNull();
|
|
document.cookie = 'tessera_desktop_client=1.2.0%7Ca6d1a64%7Cwindows; path=/';
|
|
expect(getDesktopClientInfo()).toEqual({ version: '1.2.0', commit: 'a6d1a64', os: 'windows' });
|
|
});
|
|
|
|
it('getDesktopClientInfo() ohne document: null', () => {
|
|
vi.stubGlobal('document', undefined);
|
|
expect(getDesktopClientInfo()).toBeNull();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
});
|