2024-08-27 02:44:08 +02:00
|
|
|
import { Observable, ReplaySubject, concatMap, filter, firstValueFrom, map, timeout } from "rxjs";
|
2023-11-29 15:59:50 +01:00
|
|
|
|
2024-01-10 16:36:19 +01:00
|
|
|
import {
|
|
|
|
DerivedState,
|
|
|
|
GlobalState,
|
|
|
|
SingleUserState,
|
|
|
|
ActiveUserState,
|
|
|
|
KeyDefinition,
|
2024-02-13 21:26:56 +01:00
|
|
|
DeriveDefinition,
|
2024-02-26 17:28:40 +01:00
|
|
|
UserKeyDefinition,
|
2024-01-10 16:36:19 +01:00
|
|
|
} from "../src/platform/state";
|
2023-11-29 15:59:50 +01:00
|
|
|
// eslint-disable-next-line import/no-restricted-paths -- using unexposed options for clean typing in test class
|
|
|
|
import { StateUpdateOptions } from "../src/platform/state/state-update-options";
|
2023-12-05 16:20:16 +01:00
|
|
|
// eslint-disable-next-line import/no-restricted-paths -- using unexposed options for clean typing in test class
|
2024-01-10 16:36:19 +01:00
|
|
|
import { CombinedState, activeMarker } from "../src/platform/state/user-state";
|
2023-11-29 15:59:50 +01:00
|
|
|
import { UserId } from "../src/types/guid";
|
2024-02-13 21:26:56 +01:00
|
|
|
import { DerivedStateDependencies } from "../src/types/state";
|
2023-11-29 15:59:50 +01:00
|
|
|
|
2024-01-10 16:36:19 +01:00
|
|
|
import { FakeAccountService } from "./fake-account-service";
|
|
|
|
|
2023-11-29 15:59:50 +01:00
|
|
|
const DEFAULT_TEST_OPTIONS: StateUpdateOptions<any, any> = {
|
|
|
|
shouldUpdate: () => true,
|
|
|
|
combineLatestWith: null,
|
|
|
|
msTimeout: 10,
|
|
|
|
};
|
|
|
|
|
|
|
|
function populateOptionsWithDefault(
|
2023-11-29 22:15:20 +01:00
|
|
|
options: StateUpdateOptions<any, any>,
|
2023-11-29 15:59:50 +01:00
|
|
|
): StateUpdateOptions<any, any> {
|
|
|
|
return {
|
|
|
|
...DEFAULT_TEST_OPTIONS,
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export class FakeGlobalState<T> implements GlobalState<T> {
|
|
|
|
// eslint-disable-next-line rxjs/no-exposed-subjects -- exposed for testing setup
|
|
|
|
stateSubject = new ReplaySubject<T>(1);
|
|
|
|
|
2024-01-10 16:36:19 +01:00
|
|
|
constructor(initialValue?: T) {
|
|
|
|
this.stateSubject.next(initialValue ?? null);
|
|
|
|
}
|
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
nextState(state: T) {
|
|
|
|
this.stateSubject.next(state);
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:58:31 +01:00
|
|
|
async update<TCombine>(
|
2023-11-29 15:59:50 +01:00
|
|
|
configureState: (state: T, dependency: TCombine) => T,
|
2023-11-29 22:15:20 +01:00
|
|
|
options?: StateUpdateOptions<T, TCombine>,
|
2024-02-27 22:58:31 +01:00
|
|
|
): Promise<T> {
|
2023-11-29 15:59:50 +01:00
|
|
|
options = populateOptionsWithDefault(options);
|
|
|
|
if (this.stateSubject["_buffer"].length == 0) {
|
|
|
|
// throw a more helpful not initialized error
|
|
|
|
throw new Error(
|
2023-11-29 22:15:20 +01:00
|
|
|
"You must initialize the state with a value before calling update. Try calling `stateSubject.next(initialState)` before calling update",
|
2023-11-29 15:59:50 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
const current = await firstValueFrom(this.state$.pipe(timeout(100)));
|
|
|
|
const combinedDependencies =
|
|
|
|
options.combineLatestWith != null
|
|
|
|
? await firstValueFrom(options.combineLatestWith.pipe(timeout(options.msTimeout)))
|
|
|
|
: null;
|
|
|
|
if (!options.shouldUpdate(current, combinedDependencies)) {
|
2023-12-06 20:14:49 +01:00
|
|
|
return current;
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
const newState = configureState(current, combinedDependencies);
|
|
|
|
this.stateSubject.next(newState);
|
2024-01-10 16:36:19 +01:00
|
|
|
this.nextMock(newState);
|
2023-11-29 15:59:50 +01:00
|
|
|
return newState;
|
2024-02-27 22:58:31 +01:00
|
|
|
}
|
2024-03-04 21:33:25 +01:00
|
|
|
|
2024-02-23 15:21:18 +01:00
|
|
|
/** Tracks update values resolved by `FakeState.update` */
|
2024-01-10 16:36:19 +01:00
|
|
|
nextMock = jest.fn<void, [T]>();
|
2023-11-29 15:59:50 +01:00
|
|
|
|
|
|
|
get state$() {
|
|
|
|
return this.stateSubject.asObservable();
|
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
|
|
|
|
private _keyDefinition: KeyDefinition<T> | null = null;
|
|
|
|
get keyDefinition() {
|
|
|
|
if (this._keyDefinition == null) {
|
|
|
|
throw new Error(
|
|
|
|
"Key definition not yet set, usually this means your sut has not asked for this state yet",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return this._keyDefinition;
|
|
|
|
}
|
|
|
|
set keyDefinition(value: KeyDefinition<T>) {
|
|
|
|
this._keyDefinition = value;
|
|
|
|
}
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
|
2024-01-10 16:36:19 +01:00
|
|
|
export class FakeSingleUserState<T> implements SingleUserState<T> {
|
2023-11-29 15:59:50 +01:00
|
|
|
// eslint-disable-next-line rxjs/no-exposed-subjects -- exposed for testing setup
|
2024-08-27 02:44:08 +02:00
|
|
|
stateSubject = new ReplaySubject<{
|
|
|
|
syncValue: boolean;
|
|
|
|
combinedState: CombinedState<T>;
|
|
|
|
}>(1);
|
2024-01-04 22:30:20 +01:00
|
|
|
|
|
|
|
state$: Observable<T>;
|
|
|
|
combinedState$: Observable<CombinedState<T>>;
|
|
|
|
|
2024-01-10 16:36:19 +01:00
|
|
|
constructor(
|
|
|
|
readonly userId: UserId,
|
|
|
|
initialValue?: T,
|
2024-08-27 02:44:08 +02:00
|
|
|
updateSyncCallback?: (userId: UserId, newValue: T) => Promise<void>,
|
2024-01-10 16:36:19 +01:00
|
|
|
) {
|
2024-08-27 02:44:08 +02:00
|
|
|
// Inform the state provider of updates to keep active user states in sync
|
|
|
|
this.stateSubject
|
|
|
|
.pipe(
|
|
|
|
filter((next) => next.syncValue),
|
|
|
|
concatMap(async ({ combinedState }) => {
|
|
|
|
await updateSyncCallback?.(...combinedState);
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.subscribe();
|
|
|
|
this.nextState(initialValue ?? null, { syncValue: initialValue != null });
|
|
|
|
|
|
|
|
this.combinedState$ = this.stateSubject.pipe(map((v) => v.combinedState));
|
2024-01-04 22:30:20 +01:00
|
|
|
this.state$ = this.combinedState$.pipe(map(([_userId, state]) => state));
|
|
|
|
}
|
2023-11-29 15:59:50 +01:00
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
nextState(state: T, { syncValue }: { syncValue: boolean } = { syncValue: true }) {
|
|
|
|
this.stateSubject.next({
|
|
|
|
syncValue,
|
|
|
|
combinedState: [this.userId, state],
|
|
|
|
});
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async update<TCombine>(
|
2023-11-29 15:59:50 +01:00
|
|
|
configureState: (state: T, dependency: TCombine) => T,
|
2023-11-29 22:15:20 +01:00
|
|
|
options?: StateUpdateOptions<T, TCombine>,
|
2024-01-10 16:36:19 +01:00
|
|
|
): Promise<T> {
|
2023-11-29 15:59:50 +01:00
|
|
|
options = populateOptionsWithDefault(options);
|
|
|
|
const current = await firstValueFrom(this.state$.pipe(timeout(options.msTimeout)));
|
|
|
|
const combinedDependencies =
|
|
|
|
options.combineLatestWith != null
|
|
|
|
? await firstValueFrom(options.combineLatestWith.pipe(timeout(options.msTimeout)))
|
|
|
|
: null;
|
|
|
|
if (!options.shouldUpdate(current, combinedDependencies)) {
|
2023-12-06 20:14:49 +01:00
|
|
|
return current;
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
const newState = configureState(current, combinedDependencies);
|
2024-08-27 02:44:08 +02:00
|
|
|
this.nextState(newState);
|
2024-01-10 16:36:19 +01:00
|
|
|
this.nextMock(newState);
|
2023-11-29 15:59:50 +01:00
|
|
|
return newState;
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
2023-11-29 15:59:50 +01:00
|
|
|
|
2024-02-23 15:21:18 +01:00
|
|
|
/** Tracks update values resolved by `FakeState.update` */
|
2024-01-10 16:36:19 +01:00
|
|
|
nextMock = jest.fn<void, [T]>();
|
2024-02-26 17:28:40 +01:00
|
|
|
private _keyDefinition: UserKeyDefinition<T> | null = null;
|
2024-01-10 16:36:19 +01:00
|
|
|
get keyDefinition() {
|
|
|
|
if (this._keyDefinition == null) {
|
|
|
|
throw new Error(
|
|
|
|
"Key definition not yet set, usually this means your sut has not asked for this state yet",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return this._keyDefinition;
|
|
|
|
}
|
2024-02-26 17:28:40 +01:00
|
|
|
set keyDefinition(value: UserKeyDefinition<T>) {
|
2024-01-10 16:36:19 +01:00
|
|
|
this._keyDefinition = value;
|
2023-12-05 16:20:16 +01:00
|
|
|
}
|
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
export class FakeActiveUserState<T> implements ActiveUserState<T> {
|
2023-12-05 16:20:16 +01:00
|
|
|
[activeMarker]: true;
|
2024-01-10 16:36:19 +01:00
|
|
|
|
|
|
|
// eslint-disable-next-line rxjs/no-exposed-subjects -- exposed for testing setup
|
2024-08-27 02:44:08 +02:00
|
|
|
stateSubject = new ReplaySubject<{
|
|
|
|
syncValue: boolean;
|
|
|
|
combinedState: CombinedState<T>;
|
|
|
|
}>(1);
|
2024-01-10 16:36:19 +01:00
|
|
|
|
|
|
|
state$: Observable<T>;
|
|
|
|
combinedState$: Observable<CombinedState<T>>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private accountService: FakeAccountService,
|
|
|
|
initialValue?: T,
|
2024-08-27 02:44:08 +02:00
|
|
|
updateSyncCallback?: (userId: UserId, newValue: T) => Promise<void>,
|
2024-01-10 16:36:19 +01:00
|
|
|
) {
|
2024-08-27 02:44:08 +02:00
|
|
|
// Inform the state provider of updates to keep single user states in sync
|
|
|
|
this.stateSubject.pipe(
|
|
|
|
filter((next) => next.syncValue),
|
|
|
|
concatMap(async ({ combinedState }) => {
|
|
|
|
await updateSyncCallback?.(...combinedState);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
this.nextState(initialValue ?? null, { syncValue: initialValue != null });
|
|
|
|
|
|
|
|
this.combinedState$ = this.stateSubject.pipe(map((v) => v.combinedState));
|
2024-01-10 16:36:19 +01:00
|
|
|
this.state$ = this.combinedState$.pipe(map(([_userId, state]) => state));
|
|
|
|
}
|
|
|
|
|
|
|
|
get userId() {
|
|
|
|
return this.accountService.activeUserId;
|
|
|
|
}
|
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
nextState(state: T, { syncValue }: { syncValue: boolean } = { syncValue: true }) {
|
|
|
|
this.stateSubject.next({
|
|
|
|
syncValue,
|
|
|
|
combinedState: [this.userId, state],
|
|
|
|
});
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async update<TCombine>(
|
|
|
|
configureState: (state: T, dependency: TCombine) => T,
|
|
|
|
options?: StateUpdateOptions<T, TCombine>,
|
2024-02-08 20:54:15 +01:00
|
|
|
): Promise<[UserId, T]> {
|
2024-01-10 16:36:19 +01:00
|
|
|
options = populateOptionsWithDefault(options);
|
|
|
|
const current = await firstValueFrom(this.state$.pipe(timeout(options.msTimeout)));
|
|
|
|
const combinedDependencies =
|
|
|
|
options.combineLatestWith != null
|
|
|
|
? await firstValueFrom(options.combineLatestWith.pipe(timeout(options.msTimeout)))
|
|
|
|
: null;
|
|
|
|
if (!options.shouldUpdate(current, combinedDependencies)) {
|
2024-02-08 20:54:15 +01:00
|
|
|
return [this.userId, current];
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
|
|
|
const newState = configureState(current, combinedDependencies);
|
2024-08-27 02:44:08 +02:00
|
|
|
this.nextState(newState);
|
2024-02-06 17:35:22 +01:00
|
|
|
this.nextMock([this.userId, newState]);
|
2024-02-08 20:54:15 +01:00
|
|
|
return [this.userId, newState];
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
2024-02-23 15:21:18 +01:00
|
|
|
/** Tracks update values resolved by `FakeState.update` */
|
2024-02-06 17:35:22 +01:00
|
|
|
nextMock = jest.fn<void, [[UserId, T]]>();
|
2024-01-10 16:36:19 +01:00
|
|
|
|
2024-02-26 17:28:40 +01:00
|
|
|
private _keyDefinition: UserKeyDefinition<T> | null = null;
|
2024-01-10 16:36:19 +01:00
|
|
|
get keyDefinition() {
|
|
|
|
if (this._keyDefinition == null) {
|
|
|
|
throw new Error(
|
|
|
|
"Key definition not yet set, usually this means your sut has not asked for this state yet",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return this._keyDefinition;
|
|
|
|
}
|
2024-02-26 17:28:40 +01:00
|
|
|
set keyDefinition(value: UserKeyDefinition<T>) {
|
2024-01-10 16:36:19 +01:00
|
|
|
this._keyDefinition = value;
|
2024-01-04 22:30:20 +01:00
|
|
|
}
|
2023-12-05 16:20:16 +01:00
|
|
|
}
|
2024-01-04 20:47:49 +01:00
|
|
|
|
2024-02-13 21:26:56 +01:00
|
|
|
export class FakeDerivedState<TFrom, TTo, TDeps extends DerivedStateDependencies>
|
|
|
|
implements DerivedState<TTo>
|
|
|
|
{
|
2024-01-04 20:47:49 +01:00
|
|
|
// eslint-disable-next-line rxjs/no-exposed-subjects -- exposed for testing setup
|
2024-02-13 21:26:56 +01:00
|
|
|
stateSubject = new ReplaySubject<TTo>(1);
|
2024-01-04 20:47:49 +01:00
|
|
|
|
2024-02-13 21:26:56 +01:00
|
|
|
constructor(
|
|
|
|
parentState$: Observable<TFrom>,
|
|
|
|
deriveDefinition: DeriveDefinition<TFrom, TTo, TDeps>,
|
|
|
|
dependencies: TDeps,
|
|
|
|
) {
|
|
|
|
parentState$
|
|
|
|
.pipe(
|
|
|
|
concatMap(async (v) => {
|
|
|
|
const newState = deriveDefinition.derive(v, dependencies);
|
|
|
|
if (newState instanceof Promise) {
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
return Promise.resolve(newState);
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.subscribe((newState) => {
|
|
|
|
this.stateSubject.next(newState);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
forceValue(value: TTo): Promise<TTo> {
|
2024-01-04 20:47:49 +01:00
|
|
|
this.stateSubject.next(value);
|
|
|
|
return Promise.resolve(value);
|
|
|
|
}
|
|
|
|
forceValueMock = this.forceValue as jest.MockedFunction<typeof this.forceValue>;
|
|
|
|
|
|
|
|
get state$() {
|
|
|
|
return this.stateSubject.asObservable();
|
|
|
|
}
|
|
|
|
}
|