Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ module.exports = {
collectCoverageFrom: [
"packages/protocol/**/*.{js,jsx,ts,tsx}",
"packages/shared/**/*.{js,jsx,ts,tsx}",
"packages/replay-next/**/*.{js,jsx,ts,tsx}",
"src/**/*.{js,jsx,ts,tsx}",
"!**/*.d.ts",
"!**/node_modules/**",
"!**/fixtures/**",
"!packages/shared/graphql/generated/*.ts",
],
// "text" = per-file table (no totals). Put "text-summary" last so overall % sits just above "Test Suites: …"
// (if summary is first, it scrolls away above thousands of table lines in the terminal).
coverageReporters: ["text", "text-summary"],
moduleNameMapper: {
"^devtools/(.*)": "<rootDir>/src/devtools/$1",
"^highlighter/(.*)": "<rootDir>/src/highlighter/$1",
Expand Down
2 changes: 2 additions & 0 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions packages/protocol/evaluation-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { getFunctionBody, replaceMultipleStrings } from "./evaluation-utils";

describe("evaluation-utils", () => {
describe("getFunctionBody", () => {
it("returns inner statements without the wrapping function braces", () => {
const fn = function example() {
const n = 1;
return n + 1;
};
const body = getFunctionBody(fn);
expect(body).toContain("const n = 1");
expect(body).toContain("return n + 1");
expect(body.trim().startsWith("function")).toBe(false);
});

it("strips line comments from the serialized function", () => {
const fn = function withComment() {
// should disappear
return 42;
};
const body = getFunctionBody(fn);
expect(body).not.toMatch(/\/\/ should disappear/);
expect(body).toContain("return 42");
});

it("strips block comments from the serialized function", () => {
const fn = function withBlock() {
/* block */
return 7;
};
const body = getFunctionBody(fn);
expect(body).not.toMatch(/\/\* block \*\//);
expect(body).toContain("return 7");
});
});

describe("replaceMultipleStrings", () => {
it("applies each replacement in order", () => {
expect(
replaceMultipleStrings("alpha beta", {
alpha: "A",
beta: "B",
})
).toBe("A B");
});

it("only replaces the first occurrence per search string (non-global)", () => {
expect(replaceMultipleStrings("x x", { x: "y" })).toBe("y x");
});
});
});
29 changes: 29 additions & 0 deletions packages/protocol/execution-point-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { comparePoints, pointEquals, pointPrecedes } from "./execution-point-utils";

describe("execution-point-utils", () => {
describe("pointEquals", () => {
it("returns true for identical string points", () => {
expect(pointEquals("100", "100")).toBe(true);
});

it("returns false for different points", () => {
expect(pointEquals("100", "101")).toBe(false);
});
});

describe("pointPrecedes / comparePoints", () => {
it("orders by length first (shorter numeric string is smaller)", () => {
expect(pointPrecedes("9", "10")).toBe(true);
expect(pointPrecedes("10", "9")).toBe(false);
expect(comparePoints("9", "10")).toBe(-1);
expect(comparePoints("10", "9")).toBe(1);
});

it("uses lexicographic order when lengths match", () => {
expect(comparePoints("12", "34")).toBe(-1);
expect(comparePoints("34", "12")).toBe(1);
expect(comparePoints("99", "99")).toBe(0);
expect(pointPrecedes("12", "34")).toBe(true);
});
});
});
2 changes: 1 addition & 1 deletion packages/protocol/execution-point-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ExecutionPoint } from "@replayio/protocol";
import { compareNumericStrings } from "protocol/utils";

export function pointEquals(p1: ExecutionPoint, p2: ExecutionPoint) {
p1 == p2;
return p1 === p2;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always returned undefined. Fixed to return boolean

}

export function pointPrecedes(p1: ExecutionPoint, p2: ExecutionPoint) {
Expand Down
42 changes: 42 additions & 0 deletions packages/replay-next/src/utils/url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getRelativePath, getRelativePathWithoutFile, parseUrlMemoized } from "./url";

describe("replay-next url utils", () => {
beforeEach(() => {
parseUrlMemoized.cache?.clear?.();
});

describe("parseUrlMemoized", () => {
it("rewrites webpack://_N_E prefix for parsing", () => {
const parsed = parseUrlMemoized("webpack://_N_E/./src/page.tsx");
expect(parsed.protocol).toBe("webpack:");
expect(parsed.pathname).toContain("src");
});

it("rewrites webpack-internal prefix for parsing", () => {
const parsed = parseUrlMemoized("webpack-internal:///./foo.ts");
expect(parsed.protocol).toBe("webpack-internal:");
});

it("falls back to treating a bare filename as pathname when URL parsing throws", () => {
const parsed = parseUrlMemoized("not-a-valid-url");
expect(parsed.pathname).toBe("not-a-valid-url");
expect(parsed.path).toBe("not-a-valid-url");
});
});

describe("getRelativePath", () => {
it("returns path after the first slash in pathname", () => {
expect(getRelativePath("https://example.com/a/b/c")).toBe("a/b/c");
});

it("returns the input url when parsed pathname is empty", () => {
expect(getRelativePath("")).toBe("");
});
});

describe("getRelativePathWithoutFile", () => {
it("strips the final path segment (file name)", () => {
expect(getRelativePathWithoutFile("https://example.com/pkg/src/file.ts")).toBe("pkg/src");
});
});
});
38 changes: 38 additions & 0 deletions packages/shared/utils/compare.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { arrayShallowEqual, shallowEqual, strictEqual } from "./compare";

describe("compare", () => {
describe("strictEqual", () => {
it("uses === semantics", () => {
expect(strictEqual(1, 1)).toBe(true);
expect(strictEqual(1, "1" as unknown as number)).toBe(false);
expect(strictEqual(NaN, NaN)).toBe(false);
});
});

describe("shallowEqual", () => {
it("compares primitives with ===", () => {
expect(shallowEqual(3, 3)).toBe(true);
expect(shallowEqual(3, 4)).toBe(false);
});

it("compares arrays element-wise with ===", () => {
expect(shallowEqual([1, "a"], [1, "a"])).toBe(true);
expect(shallowEqual([1, {}], [1, {}])).toBe(false);
expect(shallowEqual([1], [1, 2])).toBe(false);
});

it("compares plain objects by same keys in insertion order and values with ===", () => {
expect(shallowEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true);
expect(shallowEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false);
expect(shallowEqual({ a: {} }, { a: {} })).toBe(false);
});
});

describe("arrayShallowEqual", () => {
it("requires same length and strict element equality", () => {
expect(arrayShallowEqual([], [])).toBe(true);
expect(arrayShallowEqual([0, 1], [0, 1])).toBe(true);
expect(arrayShallowEqual([0], [0, 1])).toBe(false);
});
});
});
45 changes: 45 additions & 0 deletions packages/shared/utils/os.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { getOS, isMacOS, isWindowsOS } from "./os";

describe("os", () => {
const originalUA = window.navigator.userAgent;

afterEach(() => {
Object.defineProperty(window.navigator, "userAgent", {
value: originalUA,
configurable: true,
});
});

function setUserAgent(ua: string) {
Object.defineProperty(window.navigator, "userAgent", {
value: ua,
configurable: true,
});
}

it("maps Linux user agents", () => {
setUserAgent("Mozilla/5.0 (X11; Linux x86_64)");
expect(getOS()).toBe("Linux");
expect(isMacOS()).toBe(false);
expect(isWindowsOS()).toBe(false);
});

it("maps Windows user agents", () => {
setUserAgent("Mozilla/5.0 (Windows NT 10.0)");
expect(getOS()).toBe("WINNT");
expect(isWindowsOS()).toBe(true);
expect(isMacOS()).toBe(false);
});

it("maps macOS user agents", () => {
setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)");
expect(getOS()).toBe("Darwin");
expect(isMacOS()).toBe(true);
expect(isWindowsOS()).toBe(false);
});

it("returns Unknown for unrecognized agents", () => {
setUserAgent("SomeOtherClient/1.0");
expect(getOS()).toBe("Unknown");
});
});
31 changes: 31 additions & 0 deletions packages/shared/utils/points.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isValidPoint } from "./points";

describe("isValidPoint", () => {
it("accepts objects with required own properties", () => {
expect(
isValidPoint({
badge: null,
condition: "",
content: "",
})
).toBe(true);
});

it("rejects primitives and null", () => {
expect(isValidPoint(null)).toBe(false);
expect(isValidPoint(undefined)).toBe(false);
expect(isValidPoint("x")).toBe(false);
});

it("rejects objects missing a required key", () => {
expect(isValidPoint({ badge: 1, condition: "" })).toBe(false);
expect(isValidPoint({ badge: 1, content: "" })).toBe(false);
expect(isValidPoint({ condition: "", content: "" })).toBe(false);
});

it("rejects objects that only inherit required keys from the prototype", () => {
const proto = { badge: 1, condition: "", content: "" };
const o = Object.create(proto);
expect(isValidPoint(o)).toBe(false);
});
});
98 changes: 98 additions & 0 deletions packages/shared/utils/recording.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
jest.mock("slugify", () => ({
__esModule: true,
default: (s: string) => s,
}));

jest.mock("replay-next/src/suspense/BuildIdCache", () => ({
RecordingTarget: {
gecko: "gecko",
chromium: "chromium",
node: "node",
unknown: "unknown",
},
getRecordingTarget: jest.fn(),
}));

import { RecordingTarget, getRecordingTarget } from "replay-next/src/suspense/BuildIdCache";
import type { Recording } from "shared/graphql/types";
import { SLUG_SEPARATOR } from "shared/utils/slug";

import { extractRecordingIdFromPathname, getRecordingURL, showDurationWarning } from "./recording";

function minimalRecording(overrides: Partial<Recording> = {}): Recording {
return {
date: "2020-01-01",
duration: null,
id: "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee",
testRunId: null,
...overrides,
};
}

describe("recording", () => {
const getRecordingTargetMock = getRecordingTarget as jest.MockedFunction<
typeof getRecordingTarget
>;

beforeEach(() => {
getRecordingTargetMock.mockReset();
});

describe("extractRecordingIdFromPathname", () => {
it("extracts UUID from /recording/:id", () => {
const id = "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee";
expect(extractRecordingIdFromPathname(`/recording/${id}`)).toBe(id);
});

it("extracts id from slug--uuid under /recording/", () => {
const id = "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee";
const path = `/recording/title${SLUG_SEPARATOR}${id}`;
expect(extractRecordingIdFromPathname(path)).toBe(id);
});

it("returns undefined when not under /recording/", () => {
expect(
extractRecordingIdFromPathname(`/${"aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee"}`)
).toBeUndefined();
expect(extractRecordingIdFromPathname("/")).toBeUndefined();
});
});

describe("showDurationWarning", () => {
it("is true only when duration exceeds two minutes in ms", () => {
expect(showDurationWarning(minimalRecording({ duration: 120_001 }))).toBe(true);
expect(showDurationWarning(minimalRecording({ duration: 120_000 }))).toBe(false);
expect(showDurationWarning(minimalRecording({ duration: null }))).toBe(false);
expect(showDurationWarning(minimalRecording({ duration: 0 }))).toBe(false);
});
});

describe("getRecordingURL", () => {
it("uses legacy host for gecko targets", () => {
getRecordingTargetMock.mockReturnValue(RecordingTarget.gecko);
const rec = minimalRecording({ buildId: "firefox-gecko", title: null });
expect(getRecordingURL(rec, true)).toBe(`https://legacy.replay.io/recording/${rec.id}`);
});

it("includes /recording/ path when includeBasePath is true for non-gecko", () => {
getRecordingTargetMock.mockReturnValue(RecordingTarget.chromium);
const rec = minimalRecording({ buildId: "chromium", title: null });
expect(getRecordingURL(rec, true)).toBe(`/recording/${rec.id}`);
});

it("omits recording segment when includeBasePath is false", () => {
getRecordingTargetMock.mockReturnValue(RecordingTarget.chromium);
const rec = minimalRecording({ buildId: "chromium", title: null });
expect(getRecordingURL(rec, false)).toBe(`/${rec.id}`);
});

it("prefixes id with slugified title and separator when title is set", () => {
getRecordingTargetMock.mockReturnValue(RecordingTarget.chromium);
const rec = minimalRecording({
buildId: "chromium",
title: "MyTitle",
});
expect(getRecordingURL(rec, true)).toBe(`/recording/mytitle${SLUG_SEPARATOR}${rec.id}`);
});
});
});
Loading
Loading