Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
185 lines
6.1 KiB
TypeScript
185 lines
6.1 KiB
TypeScript
import type { Serialized } from '@rocket.chat/core-typings';
|
|
import type { ServerMethodName, ServerMethodParameters, ServerMethodReturn } from '@rocket.chat/ddp-client';
|
|
import type { Method, OperationParams, OperationResult, PathFor, PathPattern } from '@rocket.chat/rest-typings';
|
|
import type { UploadResult } from '@rocket.chat/ui-contexts';
|
|
import { ServerContext } from '@rocket.chat/ui-contexts';
|
|
import type { ContextType, ReactNode } from 'react';
|
|
import { useContext, useMemo } from 'react';
|
|
import { action } from 'storybook/actions';
|
|
|
|
const logAction = action('ServerContext');
|
|
|
|
const randomDelay = (): Promise<UploadResult> => new Promise((resolve) => setTimeout(resolve, Math.random() * 1000));
|
|
|
|
const uploadToEndpoint = (endpoint: PathFor<'POST'>, formData: any): Promise<UploadResult> =>
|
|
Promise.resolve(logAction('uploadToEndpoint', endpoint, formData)).then(randomDelay);
|
|
|
|
const getStream = (
|
|
streamName: string,
|
|
options: {
|
|
retransmit?: boolean | undefined;
|
|
retransmitToSelf?: boolean | undefined;
|
|
} = {},
|
|
): (<TEvent extends unknown[]>(eventName: string, callback: (...event: TEvent) => void) => () => void) => {
|
|
logAction('getStream', streamName, options);
|
|
|
|
return (eventName: string, callback: () => void): (() => void) => {
|
|
const subId = Math.random().toString(16).slice(2);
|
|
logAction('getStream.subscribe', streamName, eventName, subId);
|
|
|
|
randomDelay().then(() => callback());
|
|
|
|
return (): void => {
|
|
logAction('getStream.unsubscribe', streamName, eventName, subId);
|
|
};
|
|
};
|
|
};
|
|
|
|
type Operations = {
|
|
[
|
|
TOperation in Method extends infer TMethod
|
|
? TMethod extends Method
|
|
? PathPattern extends infer TPathPattern
|
|
? TPathPattern extends PathPattern
|
|
? {
|
|
id: `${TMethod} ${TPathPattern extends `/${string}` ? TPathPattern : `/v1/${TPathPattern}`}`;
|
|
fn: (
|
|
params: void extends OperationParams<TMethod, TPathPattern> ? void : OperationParams<TMethod, TPathPattern>,
|
|
) => Promise<
|
|
void extends OperationResult<TMethod, TPathPattern> ? Serialized<OperationResult<TMethod, TPathPattern>> : void
|
|
>;
|
|
}
|
|
: never
|
|
: never
|
|
: never
|
|
: never as TOperation['id']
|
|
]: TOperation['fn'];
|
|
};
|
|
|
|
export type ServerContextMockProps = Omit<Partial<ContextType<typeof ServerContext>>, 'callEndpoint' | 'callMethod'> & {
|
|
children: ReactNode;
|
|
baseURL?: string | URL;
|
|
callEndpoint?: {
|
|
[TOperationID in keyof Operations]?: Operations[TOperationID] | 'infinite' | 'errored';
|
|
};
|
|
callMethod?: {
|
|
[TMethodName in ServerMethodName]?:
|
|
((...args: ServerMethodParameters<TMethodName>) => Promise<ServerMethodReturn<TMethodName>>) | 'infinite' | 'errored';
|
|
};
|
|
};
|
|
|
|
const ServerContextMock = ({ children, baseURL, callEndpoint = {}, callMethod = {}, ...overrides }: ServerContextMockProps) => {
|
|
const parent = useContext(ServerContext);
|
|
|
|
const value = useMemo((): ContextType<typeof ServerContext> => {
|
|
type ServerContextValue = ContextType<typeof ServerContext>;
|
|
|
|
const absoluteURL: ServerContextValue['absoluteUrl'] = (path): string => {
|
|
logAction('absoluteUrl', path);
|
|
try {
|
|
return new URL(path, baseURL).toString();
|
|
} catch (e) {
|
|
return path;
|
|
}
|
|
};
|
|
|
|
const mockedEndpoints = Object.entries(callEndpoint).map(
|
|
([operationID, handler]): {
|
|
match: (method: string, path: string) => boolean;
|
|
handler: ((params: any) => Promise<unknown>) | 'infinite' | 'errored' | undefined;
|
|
} => {
|
|
const [_method, _pathPattern] = operationID.split(' ');
|
|
return {
|
|
match: (method: string, pathPattern: string): boolean => _method === method && _pathPattern === pathPattern,
|
|
handler: handler as any,
|
|
};
|
|
},
|
|
);
|
|
|
|
const _callEndpoint: ServerContextValue['callEndpoint'] = async <TMethod extends Method, TPathPattern extends PathPattern>({
|
|
method,
|
|
pathPattern,
|
|
params,
|
|
}: {
|
|
method: TMethod;
|
|
pathPattern: TPathPattern;
|
|
params: OperationParams<TMethod, TPathPattern>;
|
|
}): Promise<Serialized<OperationResult<TMethod, TPathPattern>>> => {
|
|
const mockedEndpoint = mockedEndpoints.find((endpoint) => endpoint.match(method, pathPattern));
|
|
const handler = mockedEndpoint?.handler;
|
|
|
|
if (!handler) {
|
|
logAction('callEndpoint (undefined)', method, pathPattern, params);
|
|
return undefined as any;
|
|
}
|
|
|
|
if (handler === 'infinite') {
|
|
logAction('callEndpoint (infinite)', method, pathPattern, params);
|
|
return new Promise(() => undefined);
|
|
}
|
|
|
|
if (handler === 'errored') {
|
|
logAction('callEndpoint (errored)', method, pathPattern, params);
|
|
throw new Error(`${method} ${pathPattern} failed`);
|
|
}
|
|
|
|
logAction('callEndpoint (intercepted)', method, pathPattern, params);
|
|
return handler(params) as Promise<Serialized<OperationResult<TMethod, TPathPattern>>>;
|
|
};
|
|
|
|
const _callMethod: ServerContextValue['callMethod'] = async <MethodName extends ServerMethodName>(
|
|
methodName: MethodName,
|
|
...args: ServerMethodParameters<MethodName>
|
|
): Promise<ServerMethodReturn<MethodName>> => {
|
|
const handler = callMethod[methodName];
|
|
if (!handler) {
|
|
logAction('callMethod (undefined)', methodName, ...args);
|
|
return undefined as any;
|
|
}
|
|
|
|
if (handler === 'infinite') {
|
|
logAction('callMethod (infinite)', methodName, ...args);
|
|
return new Promise<any>(() => undefined);
|
|
}
|
|
|
|
if (handler === 'errored') {
|
|
logAction('callMethod (errored)', methodName, ...args);
|
|
throw new Error(`${methodName} failed`);
|
|
}
|
|
|
|
logAction('callMethod (intercepted)', methodName, ...args);
|
|
return handler(...args);
|
|
};
|
|
|
|
return {
|
|
...parent,
|
|
info: {
|
|
version: 'x.y.z',
|
|
build: {
|
|
platform: 'storybook',
|
|
arch: 'storybook cpu',
|
|
cpus: 1,
|
|
date: new Date().toLocaleDateString(),
|
|
nodeVersion: 'lts',
|
|
osRelease: 'x',
|
|
freeMemory: 639 * 1024,
|
|
totalMemory: 640 * 1024,
|
|
},
|
|
commit: {},
|
|
marketplaceApiVersion: 'x.y.z',
|
|
},
|
|
absoluteUrl: absoluteURL,
|
|
callEndpoint: _callEndpoint,
|
|
callMethod: _callMethod,
|
|
uploadToEndpoint,
|
|
getStream,
|
|
getStreamAll: () => () => () => undefined,
|
|
...overrides,
|
|
};
|
|
}, [baseURL, callEndpoint, callMethod, overrides, parent]);
|
|
|
|
return <ServerContext.Provider value={value}>{children}</ServerContext.Provider>;
|
|
};
|
|
|
|
export default ServerContextMock;
|