+12








fcd7c979df
Co-authored-by: Artur <145776561+arturict@users.noreply.github.com> Co-authored-by: arturict <arturict@users.noreply.github.com> Co-authored-by: Thomas <thomas@ajnart.fr> Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com> Co-authored-by: Danura30082 <danura30082+github@gmail.com> Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com> Co-authored-by: ajnart <thomas@ajnart.fr> Co-authored-by: ajnart <thomas@ajnart.dev> Co-authored-by: chenjun <chenjun@foxmail.com> Co-authored-by: Wikibear <wikibear@users.noreply.github.com> Co-authored-by: Bergasha <bergasha@gmail.com> Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com> Co-authored-by: Lucas Araujo <lucas.lra@gmail.com> Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com> Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social> Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com> Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com> Co-authored-by: thrawnx <tight.bulb9384@fastmail.com> Co-authored-by: Chris <58918656+nordada@users.noreply.github.com> Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com> Co-authored-by: Yanis <naamaneyanis@gmail.com> Co-authored-by: Wikibear <me@sej.de> Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
import { describe, expect, test, vi } from "vitest";
|
|
|
|
import type { Session } from "@homarr/auth";
|
|
import type { LoggerMessage } from "@homarr/redis";
|
|
|
|
import { logRouter } from "../log";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
getRecentAsync: vi.fn(),
|
|
subscribeAsync: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("@homarr/redis", () => ({
|
|
loggingChannel: {
|
|
getRecentAsync: mocks.getRecentAsync,
|
|
subscribeAsync: mocks.subscribeAsync,
|
|
},
|
|
}));
|
|
|
|
const session = {
|
|
user: {
|
|
id: "log-viewer",
|
|
permissions: ["other-view-logs"],
|
|
colorScheme: "light",
|
|
},
|
|
expires: new Date(0).toISOString(),
|
|
} satisfies Session;
|
|
|
|
const createMessage = (id: string, timestamp: number): LoggerMessage => ({
|
|
id,
|
|
timestamp: new Date(timestamp),
|
|
level: "info",
|
|
message: id,
|
|
});
|
|
|
|
describe("log.subscribe", () => {
|
|
test("emits history before buffered live messages without duplicates", async () => {
|
|
const historyDeferred = Promise.withResolvers<LoggerMessage[]>();
|
|
let onMessage: ((message: LoggerMessage) => void) | undefined;
|
|
const unsubscribe = vi.fn();
|
|
|
|
mocks.subscribeAsync.mockImplementation(async (callback: (message: LoggerMessage) => void) => {
|
|
onMessage = callback;
|
|
return unsubscribe;
|
|
});
|
|
mocks.getRecentAsync.mockReturnValue(historyDeferred.promise);
|
|
|
|
const caller = logRouter.createCaller({
|
|
db: null as never,
|
|
deviceType: undefined,
|
|
session,
|
|
});
|
|
const subscription = await caller.subscribe({ levels: ["info"] });
|
|
const events: object[] = [];
|
|
const observer = subscription.subscribe({
|
|
next(event) {
|
|
events.push(event);
|
|
},
|
|
});
|
|
|
|
await vi.waitFor(() => expect(mocks.getRecentAsync).toHaveBeenCalledOnce());
|
|
|
|
const retainedMessage = createMessage("retained", 1);
|
|
const liveMessage = createMessage("live", 2);
|
|
onMessage?.(retainedMessage);
|
|
onMessage?.(liveMessage);
|
|
historyDeferred.resolve([retainedMessage]);
|
|
|
|
await vi.waitFor(() =>
|
|
expect(events).toEqual([
|
|
{ type: "history", messages: [retainedMessage] },
|
|
{ type: "message", message: liveMessage },
|
|
]),
|
|
);
|
|
|
|
observer.unsubscribe();
|
|
expect(unsubscribe).toHaveBeenCalledOnce();
|
|
});
|
|
});
|