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
2 changes: 1 addition & 1 deletion MODULE.bazel.lock

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

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ let package = Package(
],
products: allTargets.map { .library(name: $0.name, targets: [$0.name]) },
dependencies: [
.package(url: "https://github.com/player-ui/playerui-swift-package.git", from: "0.15.4"),
.package(url: "https://github.com/player-ui/playerui-swift-package.git", from: "1.3.0"),
.package(url: "https://github.com/player-ui/SwiftFlipper.git", exact: "0.3.0"),
],
targets: allTargets
Expand Down
88 changes: 37 additions & 51 deletions devtools/client/flipper/src/__tests__/transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,34 @@ const INSTALLED = {
version: "1.2.3",
};

type ExecFn = (method: string, ...args: Array<unknown>) => Promise<unknown>;

/**
* `plugins-get-installed-plugins` resolves to `INSTALLED` (as if the plugin is
* already present) so tests unrelated to installation don't have to stub it.
*/
const defaultExec: ExecFn = async (method) => {
if (method === "plugins-get-installed-plugins") return [INSTALLED];
return undefined;
};

/**
* A fake `FlipperServer` whose `exec` outcomes the test controls.
*
* `plugins-get-installed-plugins` resolves to `INSTALLED` by default (as if
* the plugin is already present) so tests unrelated to installation don't
* have to stub it; pass a custom `exec` to override that.
* A fake `FlipperServer` whose `exec` outcomes the test controls; pass a
* custom `exec` to override `defaultExec`.
*/
function fakeFlipperServer(
exec: (method: string, ...args: Array<unknown>) => Promise<unknown> = async (
method,
) => {
if (method === "plugins-get-installed-plugins") return [INSTALLED];
return undefined;
},
): FlipperServer {
function fakeFlipperServer(exec: ExecFn = defaultExec): FlipperServer {
return { exec } as unknown as FlipperServer;
}

/** Access private fields for seeding — avoids real sockets/child processes
* while exercising the connection-state and plugin-activation surfaces. */
type TransportInternals = {
server: unknown;
refcount: { peek: () => { pid: number; refs: number } | null };
refcount: {
peek: () => { pid: number; refs: number } | null;
release?: () => number | null;
acquire?: () => { shouldStart: boolean; commit: (pid: number) => void };
};
owns: boolean;
host: string;
port: number;
Expand Down Expand Up @@ -137,13 +142,13 @@ describe("FlipperServerTransport", () => {
(call) => call[0] === "client-request-response",
);
expect(installCallOrder).toBeLessThan(
exec.mock.invocationCallOrder[initCall],
exec.mock.invocationCallOrder[initCall]!,
);
expect(exec).toHaveBeenCalledWith("plugins-get-installed-plugins");
});

it("sends init to a single client id when one is given", async () => {
const exec = vi.fn(fakeFlipperServer().exec);
const exec = vi.fn(defaultExec);
const transport = new FlipperServerTransport();
attach(transport, fakeFlipperServer(exec), {
connectedClientIds: ["a", "b"],
Expand All @@ -163,7 +168,7 @@ describe("FlipperServerTransport", () => {
});

it("sends init to every connected client that isn't already active when no id is given", async () => {
const exec = vi.fn(fakeFlipperServer().exec);
const exec = vi.fn(defaultExec);
const transport = new FlipperServerTransport();
attach(transport, fakeFlipperServer(exec), {
connectedClientIds: ["a", "b", "c"],
Expand All @@ -190,7 +195,7 @@ describe("FlipperServerTransport", () => {
});

it("still sends init to an explicit client id even if already active", async () => {
const exec = vi.fn(fakeFlipperServer().exec);
const exec = vi.fn(defaultExec);
const transport = new FlipperServerTransport();
attach(transport, fakeFlipperServer(exec), {
connectedClientIds: ["a"],
Expand All @@ -208,7 +213,7 @@ describe("FlipperServerTransport", () => {

it("swallows a per-client failure instead of rejecting the whole call", async () => {
vi.spyOn(console, "warn").mockImplementation(() => {});
const exec = vi.fn(async (method: string, id?: string) => {
const exec = vi.fn<ExecFn>(async (method, id) => {
if (method === "plugins-get-installed-plugins") return [INSTALLED];
if (id === "bad") throw new Error("boom");
return undefined;
Expand Down Expand Up @@ -239,7 +244,7 @@ describe("FlipperServerTransport", () => {

describe("disablePlugin", () => {
it("sends deinit to a single client id when one is given", async () => {
const exec = vi.fn(fakeFlipperServer().exec);
const exec = vi.fn(defaultExec);
const transport = new FlipperServerTransport();
attach(transport, fakeFlipperServer(exec), { activeClientIds: ["a"] });

Expand All @@ -253,7 +258,7 @@ describe("FlipperServerTransport", () => {
});

it("sends deinit only to clients currently believed active when no id is given", async () => {
const exec = vi.fn(fakeFlipperServer().exec);
const exec = vi.fn(defaultExec);
const transport = new FlipperServerTransport();
attach(transport, fakeFlipperServer(exec), {
connectedClientIds: ["a", "b"],
Expand Down Expand Up @@ -543,15 +548,10 @@ describe("FlipperServerTransport", () => {
vi.useFakeTimers();
try {
const transport = new FlipperServerTransport();
const state = internals(transport);
state.refcount = {
internals(transport).refcount = {
peek: () => null,
} as unknown as TransportInternals["refcount"];
(
transport as unknown as {
refcount: { release: () => number | null };
}
).refcount.release = () => 4242;
release: () => 4242,
};

let alive = true;
const killSpy = vi.spyOn(process, "kill").mockImplementation(((
Expand Down Expand Up @@ -591,17 +591,13 @@ describe("FlipperServerTransport", () => {
const transport = new FlipperServerTransport();

let releaseCalls = 0;
(
transport as unknown as {
refcount: { release: () => number | null };
}
).refcount = {
internals(transport).refcount = {
peek: () => null,
release: () => {
releaseCalls += 1;
return null;
},
} as unknown as TransportInternals["refcount"];
};

const first = transport.close();
const second = transport.close();
Expand Down Expand Up @@ -649,23 +645,13 @@ describe("FlipperServerTransport", () => {
// or locally, so real net.connect attempts genuinely fail fast.
port: 1,
});
const state = internals(transport);
state.refcount = {
internals(transport).refcount = {
peek: () => null,
} as unknown as TransportInternals["refcount"];
(
transport as unknown as {
refcount: {
acquire: () => {
shouldStart: boolean;
commit: (pid: number) => void;
};
};
}
).refcount.acquire = () => ({
shouldStart: true,
commit: vi.fn(),
});
acquire: () => ({
shouldStart: true,
commit: vi.fn(),
}),
};

const connectPromise = transport.connect().catch((err) => err as Error);

Expand Down
2 changes: 1 addition & 1 deletion devtools/client/react/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LogForwarder implements PlayerPlugin, ReactPlayerPlugin {
}
}

export const PUBSUB_PLUGIN = new PubSubPlugin();
export const PUBSUB_PLUGIN: PubSubPlugin = new PubSubPlugin();

export const PLAYER_PLUGINS: ReactPlayerPlugin[] = [
new CommonTypesPlugin(),
Expand Down
17 changes: 14 additions & 3 deletions devtools/client/react/src/state/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { CommunicationLayerMethods } from "@player-devtools/types";
import type {
CommunicationLayerMethods,
ExtensionState,
} from "@player-devtools/types";
import { useEffect, useMemo, useSyncExternalStore } from "react";

import { createExtensionClient } from "@player-devtools/client";
import {
createExtensionClient,
type ExtensionClient,
} from "@player-devtools/client";

/**
* Thin React adapter over `createExtensionClient`.
Expand All @@ -14,7 +20,12 @@ export const useExtensionState = ({
}: {
/** the communication layer to use for the extension */
communicationLayer: CommunicationLayerMethods;
}) => {
}): {
state: ExtensionState;
selectPlayer: ExtensionClient["selectPlayer"];
selectPlugin: ExtensionClient["selectPlugin"];
handleInteraction: ExtensionClient["handleInteraction"];
} => {
const client = useMemo(
() => createExtensionClient(communicationLayer),
[communicationLayer],
Expand Down
6 changes: 5 additions & 1 deletion devtools/flipper-plugin/src/theme/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { theme } from "flipper-plugin";
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(selectAnatomy.keys);

export const Select = defineMultiStyleConfig({
export const Select: ReturnType<
ReturnType<
typeof createMultiStyleConfigHelpers<(typeof selectAnatomy.keys)[number]>
>["defineMultiStyleConfig"]
> = defineMultiStyleConfig({
baseStyle: definePartsStyle({
field: {
background: theme.buttonDefaultBackground,
Expand Down
6 changes: 5 additions & 1 deletion devtools/flipper-plugin/src/theme/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { theme } from "flipper-plugin";
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(tableAnatomy.keys);

export const Table = defineMultiStyleConfig({
export const Table: ReturnType<
ReturnType<
typeof createMultiStyleConfigHelpers<(typeof tableAnatomy.keys)[number]>
>["defineMultiStyleConfig"]
> = defineMultiStyleConfig({
baseStyle: definePartsStyle({
table: {
lineHeight: 1,
Expand Down
4 changes: 2 additions & 2 deletions devtools/mcp/src/__tests__/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import type { Transport } from "@player-devtools/types";

import { MCPServer } from "../server";

const shutdown = vi.hoisted(() => vi.fn());
const createAnalytics = vi.hoisted(() => vi.fn());

Expand All @@ -18,8 +20,6 @@ vi.mock("@modelcontextprotocol/sdk/server/stdio.js", () => ({
},
}));

const { MCPServer } = await import("../server");

/** A transport whose connect outcome the test controls. */
function fakeTransport(connect: () => Promise<void>): Transport {
return {
Expand Down
4 changes: 2 additions & 2 deletions devtools/mcp/src/telemetry/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

import { createAnalytics } from "../index";

const instrument = vi.hoisted(() => vi.fn());
const shutdown = vi.hoisted(() => vi.fn());
const PostHogCtor = vi.hoisted(() => vi.fn());
Expand All @@ -20,8 +22,6 @@ vi.mock("posthog-node", () => ({
},
}));

const { createAnalytics } = await import("../index");

const KEY = "phc_test_key";
const server = {} as object;
const deps = { isTransportConnected: () => true };
Expand Down
8 changes: 4 additions & 4 deletions devtools/mcp/src/telemetry/__tests__/redaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("allowlistEventProperties", () => {
// Assert on the serialized payload, not key presence — a nested leak would
// pass a shallow key check.
expect(JSON.stringify(result)).not.toContain(SECRET);
expect(result.properties).toEqual({
expect(result?.properties).toEqual({
$mcp_tool_name: "invoke_action",
$mcp_duration_ms: 12,
});
Expand All @@ -42,7 +42,7 @@ describe("allowlistEventProperties", () => {
});

expect(JSON.stringify(result)).not.toContain(SECRET);
expect(Object.keys(result.properties ?? {})).toEqual(["$mcp_tool_name"]);
expect(Object.keys(result?.properties ?? {})).toEqual(["$mcp_tool_name"]);
});

it("preserves every allowlisted property", () => {
Expand All @@ -55,7 +55,7 @@ describe("allowlistEventProperties", () => {
properties,
});

expect(Object.keys(result.properties ?? {}).sort()).toEqual(
expect(Object.keys(result?.properties ?? {}).sort()).toEqual(
[...ALLOWED_EVENT_PROPERTIES].sort(),
);
});
Expand All @@ -66,7 +66,7 @@ describe("allowlistEventProperties", () => {
properties: { $mcp_parameters: SECRET, $mcp_response: SECRET },
});

expect(result.properties).toEqual({});
expect(result?.properties).toEqual({});
expect(JSON.stringify(result)).not.toContain(SECRET);
});

Expand Down
6 changes: 3 additions & 3 deletions devtools/mcp/src/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export {
export { resolveTelemetryConfig, type TelemetryConfig } from "./config";
export { getInstallId, type InstallIdDeps } from "./installId";

declare global {
const __VERSION__: string;
}
// Scoped to this module so that packages declaring the same define don't
// collide in a shared TS program.
declare const __VERSION__: string;

/**
* Stamped at release build time by tsup (see devtools/tsup.config.ts). The
Expand Down
6 changes: 4 additions & 2 deletions devtools/mcp/src/tools/__tests__/flipper.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi } from "vitest";
import type { ExtensionClient } from "@player-devtools/client";
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { FlipperServerTransport } from "@player-devtools/client-flipper";
import type { Transport } from "@player-devtools/types";

Expand All @@ -15,8 +16,9 @@ import {

const client = {} as ExtensionClient;

function parse(result: { content: Array<{ text?: string }> }): unknown {
return JSON.parse(result.content[0]?.text ?? "{}");
function parse(result: CallToolResult): unknown {
const first = result.content[0];
return JSON.parse(first?.type === "text" ? first.text : "{}");
}

/**
Expand Down
Loading