Skip to content

Commit

Permalink
chore: couple test, remove postComment refs
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jun 22, 2024
1 parent da2afe9 commit 11e750e
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 74 deletions.
18 changes: 6 additions & 12 deletions src/supabase/helpers/tables/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,55 +52,51 @@ export class Logs {
return metadata;
}

public ok(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null {
public ok(log: string, metadata?: Metadata): LogReturn | null {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LOG_LEVEL.INFO,
consoleLog: Logs.console.ok,
logMessage: log,
metadata,
postComment,
type: "ok",
});
}

public info(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null {
public info(log: string, metadata?: Metadata): LogReturn | null {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LOG_LEVEL.INFO,
consoleLog: Logs.console.info,
logMessage: log,
metadata,
postComment,
type: "info",
});
}

public error(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null {
public error(log: string, metadata?: Metadata): LogReturn | null {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LOG_LEVEL.ERROR,
consoleLog: Logs.console.error,
logMessage: log,
metadata,
postComment,
type: "error",
});
}

public debug(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null {
public debug(log: string, metadata?: Metadata): LogReturn | null {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LOG_LEVEL.DEBUG,
consoleLog: Logs.console.debug,
logMessage: log,
metadata,
postComment,
type: "debug",
});
}

public fatal(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null {
public fatal(log: string, metadata?: Metadata): LogReturn | null {
if (!metadata) {
metadata = Logs.convertErrorsIntoObjects(new Error(log)) as Metadata;
const stack = metadata.stack as string[];
Expand All @@ -121,19 +117,17 @@ export class Logs {
consoleLog: Logs.console.fatal,
logMessage: log,
metadata,
postComment,
type: "fatal",
});
}

public verbose(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null {
public verbose(log: string, metadata?: Metadata): LogReturn | null {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LOG_LEVEL.VERBOSE,
consoleLog: Logs.console.verbose,
logMessage: log,
metadata,
postComment,
type: "verbose",
});
}
Expand Down
8 changes: 5 additions & 3 deletions src/supabase/helpers/tables/pretty-logs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import util from "util";
import { Colors, Metadata, PrettyLogsWithOk } from "../../types/log-types";
import { COLORS, LOG_LEVEL } from "../../constants";

Expand Down Expand Up @@ -109,7 +108,7 @@ export class PrettyLogs {
const symbol = defaultSymbols[type];

// Formatting the message
const messageFormatted = typeof message === "string" ? message : util.inspect(message, { showHidden: true, depth: null, breakLength: Infinity });
const messageFormatted = typeof message === "string" ? message : JSON.stringify(message, null, 2);
// const messageFormatted =
// typeof message === "string" ? message : JSON.stringify(Logs.convertErrorsIntoObjects(message));

Expand All @@ -135,8 +134,11 @@ export class PrettyLogs {
};

const _console = console[colorMap[type][0] as keyof typeof console] as (...args: string[]) => void;
if (typeof _console === "function") {
if (typeof _console === "function" && fullLogString.length > 12) {
_console(this._colorizeText(fullLogString, colorMap[type][1]));
} else if (fullLogString.length <= 12) {
// removing empty logs which only contain the symbol
return;
} else {
throw new Error(fullLogString);
}
Expand Down
1 change: 0 additions & 1 deletion src/supabase/types/log-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export type LogParams = {
consoleLog: LogFunction;
logMessage: string;
metadata?: Metadata;
postComment?: boolean;
type: PrettyLogsWithOk;
};

Expand Down
32 changes: 32 additions & 0 deletions src/supabase/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const ansiEscapeCodes = /\x1b\[\d+m|\s/g;

function cleanLogs(
spy: jest.SpiedFunction<{
(...data: string[]): void;
(message?: string, ...optionalParams: string[]): void;
}>
) {
const strs = spy.mock.calls.map((call) => call.map((str) => str?.toString()).join(" "));
return strs.flat().map((str) => cleanLogString(str));
}

export function cleanLogString(logString: string) {
return logString.replaceAll(ansiEscapeCodes, "").replaceAll(/\n/g, "").replaceAll(/\r/g, "").replaceAll(/\t/g, "").trim();
}

export function cleanSpyLogs(
spy: jest.SpiedFunction<{
(...data: string[]): void;
(message?: string, ...optionalParams: string[]): void;
}>
): string[] {
return cleanLogs(spy);
}

export function tryError() {
try {
throw new Error("This is an error");
} catch (e) {
return e as Error;
}
}
Binary file removed static/fonts/ubiquity-nova-standard.eot
Binary file not shown.
Binary file removed static/fonts/ubiquity-nova-standard.ttf
Binary file not shown.
Binary file removed static/fonts/ubiquity-nova-standard.woff
Binary file not shown.
13 changes: 0 additions & 13 deletions static/index.html

This file was deleted.

10 changes: 0 additions & 10 deletions static/main.ts

This file was deleted.

16 changes: 0 additions & 16 deletions static/style.css

This file was deleted.

10 changes: 10 additions & 0 deletions tests/logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LOG_LEVEL } from "../src/supabase/constants";
import { Logs } from "../src/supabase/helpers/tables/logs";
import { LogReturn } from "../src/supabase/types/log-types";

describe("Logs", () => {
let logs: Logs;
Expand Down Expand Up @@ -37,4 +38,13 @@ describe("Logs", () => {
const logReturn = logs.verbose("This is a VERBOSE message");
expect(logReturn).not.toBeNull();
});

it("should return a LogReturn object", () => {
const logReturn: LogReturn | null = logs.ok("This is an OK message");
expect(logReturn).toBeInstanceOf(LogReturn);
const msg = logReturn?.logMessage;
const metadata = logReturn?.metadata;
expect(msg).toHaveProperty("diff");
expect(metadata).toHaveProperty("caller");
});
});
57 changes: 38 additions & 19 deletions tests/pretty-logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import { PrettyLogs } from "../src/supabase/helpers/tables/pretty-logs";

const ansiEscapeCodes = /\x1b\[\d+m|\s/g;

function cleanSpyLogs(
spy: jest.SpiedFunction<{
(...data: any[]): void;
(message?: any, ...optionalParams: any[]): void;
}>
) {
const strs = spy.mock.calls.map((call) => call.map((str) => str.toString()).join(" "));
return strs.flat().map((str) => cleanLogString(str));
}

function cleanLogString(logString: string) {
return logString.replace(ansiEscapeCodes, "").replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, "");
}
import { cleanLogString, cleanSpyLogs, tryError } from "../src/supabase/utils";

describe("PrettyLogs", () => {
let logs: PrettyLogs;
Expand Down Expand Up @@ -56,7 +41,7 @@ describe("PrettyLogs", () => {
const logReturn = logs.debug("This is a DEBUG message");
expect(logReturn).toBeUndefined();
const cleanLogStrings = cleanSpyLogs(logSpy);
expect(cleanLogStrings).toContain(cleanLogString(" ›› This is a DEBUG message"))
expect(cleanLogStrings).toContain(cleanLogString(" ›› This is a DEBUG message"));
});

it("should log a 'fatal' message", () => {
Expand All @@ -72,6 +57,40 @@ describe("PrettyLogs", () => {
const logReturn = logs.verbose("This is a VERBOSE message");
expect(logReturn).toBeUndefined();
const cleanLogStrings = cleanSpyLogs(logSpy);
expect(cleanLogStrings).toContain(cleanLogString((" 💬 This is a VERBOSE message")));
expect(cleanLogStrings).toContain(cleanLogString(" 💬 This is a VERBOSE message"));
});

it("should log metadata", () => {
const logSpy = jest.spyOn(console, "debug").mockImplementation();
const logReturn = logs.debug("This is a METADATA message", { thisIsMetadata: true, stuff: Array(5).fill("stuff"), moreStuff: { a: "a", b: "b" } });
expect(logReturn).toBeUndefined();
const cleanLogStrings = cleanSpyLogs(logSpy);
expect(cleanLogStrings).toEqual([
cleanLogString(" ›› This is a METADATA message"),
cleanLogString(` ›› ${JSON.stringify({ thisIsMetadata: true, stuff: ["stuff", "stuff", "stuff", "stuff", "stuff"], moreStuff: { a: "a", b: "b" } })}`),
]);
});

it("should log metadata as a string", () => {
const logSpy = jest.spyOn(console, "debug").mockImplementation();
const logReturn = logs.debug("This is a METADATA message", "This is metadata as a string");
expect(logReturn).toBeUndefined();
const cleanLogStrings = cleanSpyLogs(logSpy);
expect(cleanLogStrings).toEqual([cleanLogString(" ›› This is a METADATA message"), cleanLogString(" ›› This is metadata as a string")]);
});

it("should log an error and stack", () => {
const logSpy = jest.spyOn(console, "debug").mockImplementation();
const logReturn = logs.debug("This is a METADATA message", { error: tryError() });
expect(logReturn).toBeUndefined();
const cleanLogStrings = cleanSpyLogs(logSpy);

const errorRegex = /tryError\(.+\)Object.<anonymous>\(/;

expect(cleanLogStrings).toEqual([
cleanLogString(" ›› This is a METADATA message"),
cleanLogString(` ›› ${JSON.stringify({ error: {} })}`),
expect.stringMatching(errorRegex),
]);
});
});
});

0 comments on commit 11e750e

Please sign in to comment.