2024-01-10 16:36:19 +01:00
|
|
|
import { mock } from "jest-mock-extended";
|
2024-04-30 15:13:02 +02:00
|
|
|
import { ReplaySubject, combineLatest, map } from "rxjs";
|
2024-01-10 16:36:19 +01:00
|
|
|
|
|
|
|
import { AccountInfo, AccountService } from "../src/auth/abstractions/account.service";
|
|
|
|
import { UserId } from "../src/types/guid";
|
|
|
|
|
|
|
|
export function mockAccountServiceWith(
|
|
|
|
userId: UserId,
|
|
|
|
info: Partial<AccountInfo> = {},
|
2024-04-30 15:13:02 +02:00
|
|
|
activity: Record<UserId, Date> = {},
|
2024-01-10 16:36:19 +01:00
|
|
|
): FakeAccountService {
|
|
|
|
const fullInfo: AccountInfo = {
|
|
|
|
...info,
|
|
|
|
...{
|
|
|
|
name: "name",
|
|
|
|
email: "email",
|
2024-04-30 15:13:02 +02:00
|
|
|
emailVerified: true,
|
2024-01-10 16:36:19 +01:00
|
|
|
},
|
|
|
|
};
|
2024-04-30 15:13:02 +02:00
|
|
|
|
|
|
|
const fullActivity = { [userId]: new Date(), ...activity };
|
|
|
|
|
|
|
|
const service = new FakeAccountService({ [userId]: fullInfo }, fullActivity);
|
2024-01-10 16:36:19 +01:00
|
|
|
service.activeAccountSubject.next({ id: userId, ...fullInfo });
|
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class FakeAccountService implements AccountService {
|
|
|
|
mock = mock<AccountService>();
|
|
|
|
// eslint-disable-next-line rxjs/no-exposed-subjects -- test class
|
|
|
|
accountsSubject = new ReplaySubject<Record<UserId, AccountInfo>>(1);
|
|
|
|
// eslint-disable-next-line rxjs/no-exposed-subjects -- test class
|
|
|
|
activeAccountSubject = new ReplaySubject<{ id: UserId } & AccountInfo>(1);
|
2024-04-30 15:13:02 +02:00
|
|
|
// eslint-disable-next-line rxjs/no-exposed-subjects -- test class
|
|
|
|
accountActivitySubject = new ReplaySubject<Record<UserId, Date>>(1);
|
2024-01-10 16:36:19 +01:00
|
|
|
private _activeUserId: UserId;
|
|
|
|
get activeUserId() {
|
|
|
|
return this._activeUserId;
|
|
|
|
}
|
2024-04-09 17:17:00 +02:00
|
|
|
accounts$ = this.accountsSubject.asObservable();
|
|
|
|
activeAccount$ = this.activeAccountSubject.asObservable();
|
2024-04-30 15:13:02 +02:00
|
|
|
accountActivity$ = this.accountActivitySubject.asObservable();
|
|
|
|
get sortedUserIds$() {
|
|
|
|
return this.accountActivity$.pipe(
|
|
|
|
map((activity) => {
|
|
|
|
return Object.entries(activity)
|
|
|
|
.map(([userId, lastActive]: [UserId, Date]) => ({ userId, lastActive }))
|
|
|
|
.sort((a, b) => a.lastActive.getTime() - b.lastActive.getTime())
|
|
|
|
.map((a) => a.userId);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
get nextUpAccount$() {
|
|
|
|
return combineLatest([this.accounts$, this.activeAccount$, this.sortedUserIds$]).pipe(
|
|
|
|
map(([accounts, activeAccount, sortedUserIds]) => {
|
|
|
|
const nextId = sortedUserIds.find((id) => id !== activeAccount?.id && accounts[id] != null);
|
|
|
|
return nextId ? { id: nextId, ...accounts[nextId] } : null;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
|
2024-04-30 15:13:02 +02:00
|
|
|
constructor(initialData: Record<UserId, AccountInfo>, accountActivity?: Record<UserId, Date>) {
|
2024-01-10 16:36:19 +01:00
|
|
|
this.accountsSubject.next(initialData);
|
|
|
|
this.activeAccountSubject.subscribe((data) => (this._activeUserId = data?.id));
|
|
|
|
this.activeAccountSubject.next(null);
|
2024-04-30 15:13:02 +02:00
|
|
|
this.accountActivitySubject.next(accountActivity);
|
|
|
|
}
|
|
|
|
setAccountActivity(userId: UserId, lastActivity: Date): Promise<void> {
|
|
|
|
this.accountActivitySubject.next({
|
|
|
|
...this.accountActivitySubject["_buffer"][0],
|
|
|
|
[userId]: lastActivity,
|
|
|
|
});
|
|
|
|
return this.mock.setAccountActivity(userId, lastActivity);
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async addAccount(userId: UserId, accountData: AccountInfo): Promise<void> {
|
2024-02-08 18:02:48 +01:00
|
|
|
const current = this.accountsSubject["_buffer"][0] ?? {};
|
|
|
|
this.accountsSubject.next({ ...current, [userId]: accountData });
|
|
|
|
await this.mock.addAccount(userId, accountData);
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async setAccountName(userId: UserId, name: string): Promise<void> {
|
2024-02-08 18:02:48 +01:00
|
|
|
await this.mock.setAccountName(userId, name);
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async setAccountEmail(userId: UserId, email: string): Promise<void> {
|
2024-02-08 18:02:48 +01:00
|
|
|
await this.mock.setAccountEmail(userId, email);
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
|
|
|
|
2024-04-30 15:13:02 +02:00
|
|
|
async setAccountEmailVerified(userId: UserId, emailVerified: boolean): Promise<void> {
|
|
|
|
await this.mock.setAccountEmailVerified(userId, emailVerified);
|
|
|
|
}
|
|
|
|
|
2024-01-10 16:36:19 +01:00
|
|
|
async switchAccount(userId: UserId): Promise<void> {
|
2024-03-21 18:02:04 +01:00
|
|
|
const next =
|
|
|
|
userId == null ? null : { id: userId, ...this.accountsSubject["_buffer"]?.[0]?.[userId] };
|
|
|
|
this.activeAccountSubject.next(next);
|
2024-02-08 18:02:48 +01:00
|
|
|
await this.mock.switchAccount(userId);
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
2024-04-30 15:13:02 +02:00
|
|
|
|
|
|
|
async clean(userId: UserId): Promise<void> {
|
|
|
|
const current = this.accountsSubject["_buffer"][0] ?? {};
|
|
|
|
const updated = { ...current, [userId]: loggedOutInfo };
|
|
|
|
this.accountsSubject.next(updated);
|
|
|
|
await this.mock.clean(userId);
|
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
2024-04-30 15:13:02 +02:00
|
|
|
|
|
|
|
const loggedOutInfo: AccountInfo = {
|
|
|
|
name: undefined,
|
|
|
|
email: "",
|
|
|
|
emailVerified: false,
|
|
|
|
};
|