@lunatest/react 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/LunaTestProvider.d.ts +15 -0
- package/dist/LunaTestProvider.js +18 -0
- package/dist/adapters/ethers.d.ts +5 -0
- package/dist/adapters/ethers.js +10 -0
- package/dist/adapters/wagmi.d.ts +18 -0
- package/dist/adapters/wagmi.js +14 -0
- package/dist/adapters/web3js.d.ts +10 -0
- package/dist/adapters/web3js.js +7 -0
- package/dist/bootstrap.d.ts +15 -0
- package/dist/bootstrap.js +48 -0
- package/dist/devtools/LunaDevtoolsPanel.d.ts +20 -0
- package/dist/devtools/LunaDevtoolsPanel.js +148 -0
- package/dist/devtools/mount.d.ts +8 -0
- package/dist/devtools/mount.js +41 -0
- package/dist/hooks/useLunaProvider.d.ts +2 -0
- package/dist/hooks/useLunaProvider.js +5 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +11 -0
- package/dist/intercept.d.ts +6 -0
- package/dist/intercept.js +6 -0
- package/dist/luna-provider.d.ts +2 -0
- package/dist/luna-provider.js +4 -0
- package/dist/node-env.d.ts +1 -0
- package/dist/node-env.js +13 -0
- package/dist/useLunaTest.d.ts +1 -0
- package/dist/useLunaTest.js +9 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Coco
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { LunaProvider, type LunaProviderOptions } from "@lunatest/core";
|
|
3
|
+
export type LunaTestContextValue = {
|
|
4
|
+
provider: LunaProvider;
|
|
5
|
+
scenarioId?: string;
|
|
6
|
+
setScenarioId: (value?: string) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare const LunaTestContext: React.Context<LunaTestContextValue | null>;
|
|
9
|
+
export type LunaTestProviderProps = {
|
|
10
|
+
provider?: LunaProvider;
|
|
11
|
+
options?: LunaProviderOptions;
|
|
12
|
+
initialScenarioId?: string;
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
};
|
|
15
|
+
export declare function LunaTestProvider(props: LunaTestProviderProps): React.FunctionComponentElement<React.ProviderProps<LunaTestContextValue | null>>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { createContext, useMemo, useState } from "react";
|
|
2
|
+
import { createLunaProvider } from "./luna-provider.js";
|
|
3
|
+
export const LunaTestContext = createContext(null);
|
|
4
|
+
export function LunaTestProvider(props) {
|
|
5
|
+
const [scenarioId, setScenarioId] = useState(props.initialScenarioId);
|
|
6
|
+
const provider = useMemo(() => {
|
|
7
|
+
if (props.provider) {
|
|
8
|
+
return props.provider;
|
|
9
|
+
}
|
|
10
|
+
return createLunaProvider(props.options ?? {});
|
|
11
|
+
}, [props.provider, props.options]);
|
|
12
|
+
const value = useMemo(() => ({
|
|
13
|
+
provider,
|
|
14
|
+
scenarioId,
|
|
15
|
+
setScenarioId,
|
|
16
|
+
}), [provider, scenarioId]);
|
|
17
|
+
return React.createElement(LunaTestContext.Provider, { value }, props.children);
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type LunaProvider } from "@lunatest/core";
|
|
2
|
+
type Eip1193RequestLike = {
|
|
3
|
+
method: string;
|
|
4
|
+
params?: unknown[];
|
|
5
|
+
};
|
|
6
|
+
type WagmiLikeChain = {
|
|
7
|
+
id: number;
|
|
8
|
+
};
|
|
9
|
+
type WagmiLikeTransport = {
|
|
10
|
+
type: "luna";
|
|
11
|
+
request: (payload: Eip1193RequestLike) => Promise<unknown>;
|
|
12
|
+
};
|
|
13
|
+
export type WagmiLikeConfig = {
|
|
14
|
+
chains?: WagmiLikeChain[];
|
|
15
|
+
transports?: Record<number, WagmiLikeTransport>;
|
|
16
|
+
};
|
|
17
|
+
export declare function withLunaWagmiConfig(config: WagmiLikeConfig, provider: LunaProvider): WagmiLikeConfig;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function withLunaWagmiConfig(config, provider) {
|
|
2
|
+
const chainIds = (config.chains ?? [{ id: 1 }]).map((chain) => chain.id);
|
|
3
|
+
const transports = { ...(config.transports ?? {}) };
|
|
4
|
+
for (const chainId of chainIds) {
|
|
5
|
+
transports[chainId] = {
|
|
6
|
+
type: "luna",
|
|
7
|
+
request: (payload) => provider.request(payload),
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
...config,
|
|
12
|
+
transports,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type LunaProvider } from "@lunatest/core";
|
|
2
|
+
type RequestPayload = {
|
|
3
|
+
method: string;
|
|
4
|
+
params?: unknown[];
|
|
5
|
+
};
|
|
6
|
+
export type Web3JsLikeProvider = {
|
|
7
|
+
request: (payload: RequestPayload) => Promise<unknown>;
|
|
8
|
+
};
|
|
9
|
+
export declare function createWeb3JsAdapter(provider: LunaProvider): Web3JsLikeProvider;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { LuaConfig } from "@lunatest/core";
|
|
2
|
+
import { type LunaRuntimeInterceptConfig } from "@lunatest/runtime-intercept";
|
|
3
|
+
export type LunaBootstrapOptions = {
|
|
4
|
+
source?: string | URL;
|
|
5
|
+
nodeEnv?: string;
|
|
6
|
+
mountDevtools?: boolean;
|
|
7
|
+
devtoolsTargetId?: string;
|
|
8
|
+
configOverride?: Partial<LunaRuntimeInterceptConfig>;
|
|
9
|
+
};
|
|
10
|
+
export type LunaBootstrapResult = {
|
|
11
|
+
enabled: boolean;
|
|
12
|
+
unmountDevtools?: () => void;
|
|
13
|
+
config: LuaConfig;
|
|
14
|
+
};
|
|
15
|
+
export declare function bootstrapLunaRuntime(options?: LunaBootstrapOptions): Promise<LunaBootstrapResult>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { loadLunaConfig } from "@lunatest/core";
|
|
2
|
+
import { applyInterceptState, enableLunaRuntimeIntercept, setRouteMocks, } from "@lunatest/runtime-intercept";
|
|
3
|
+
import { mountLunaDevtools } from "./devtools/mount.js";
|
|
4
|
+
import { resolveNodeEnv } from "./node-env.js";
|
|
5
|
+
function toRuntimeConfig(config, configOverride) {
|
|
6
|
+
return {
|
|
7
|
+
...configOverride,
|
|
8
|
+
intercept: {
|
|
9
|
+
...configOverride?.intercept,
|
|
10
|
+
mode: configOverride?.intercept?.mode ?? config.mode,
|
|
11
|
+
mockResponses: {
|
|
12
|
+
...(config.intercept?.mockResponses ?? {}),
|
|
13
|
+
...(configOverride?.intercept?.mockResponses ?? {}),
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export async function bootstrapLunaRuntime(options = {}) {
|
|
19
|
+
const config = await loadLunaConfig(options.source ?? "./lunatest.lua");
|
|
20
|
+
const nodeEnv = resolveNodeEnv(options.nodeEnv);
|
|
21
|
+
const runtimeConfig = toRuntimeConfig(config, options.configOverride);
|
|
22
|
+
const enabled = enableLunaRuntimeIntercept(runtimeConfig, nodeEnv);
|
|
23
|
+
if (!enabled) {
|
|
24
|
+
return {
|
|
25
|
+
enabled: false,
|
|
26
|
+
config,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const routeMocks = options.configOverride?.intercept?.routes ?? config.intercept?.routes ?? [];
|
|
30
|
+
setRouteMocks(routeMocks);
|
|
31
|
+
if (config.given) {
|
|
32
|
+
applyInterceptState(config.given);
|
|
33
|
+
}
|
|
34
|
+
if (config.intercept?.state) {
|
|
35
|
+
applyInterceptState(config.intercept.state);
|
|
36
|
+
}
|
|
37
|
+
const unmountDevtools = options.mountDevtools === false
|
|
38
|
+
? undefined
|
|
39
|
+
: mountLunaDevtools({
|
|
40
|
+
targetId: options.devtoolsTargetId,
|
|
41
|
+
nodeEnv,
|
|
42
|
+
}) ?? undefined;
|
|
43
|
+
return {
|
|
44
|
+
enabled: true,
|
|
45
|
+
unmountDevtools,
|
|
46
|
+
config,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type RouteMock } from "@lunatest/runtime-intercept";
|
|
2
|
+
type LunaDevtoolsPanelProps = {
|
|
3
|
+
title?: string;
|
|
4
|
+
initialLua?: string;
|
|
5
|
+
initialRoutes?: RouteMock[];
|
|
6
|
+
initialState?: Record<string, unknown>;
|
|
7
|
+
onRunScenario?: (luaScript: string) => Promise<{
|
|
8
|
+
pass: boolean;
|
|
9
|
+
error?: string;
|
|
10
|
+
diff?: string;
|
|
11
|
+
}> | {
|
|
12
|
+
pass: boolean;
|
|
13
|
+
error?: string;
|
|
14
|
+
diff?: string;
|
|
15
|
+
};
|
|
16
|
+
onSetRouteMocks?: (routes: RouteMock[]) => void | Promise<void>;
|
|
17
|
+
onPatchState?: (patch: Record<string, unknown>) => void | Promise<void>;
|
|
18
|
+
};
|
|
19
|
+
export declare function LunaDevtoolsPanel(props: LunaDevtoolsPanelProps): import("react/jsx-runtime").JSX.Element;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useMemo, useState } from "react";
|
|
3
|
+
import { executeLuaScenario, loadLunaConfig } from "@lunatest/core";
|
|
4
|
+
import { applyInterceptState, getInterceptState, setRouteMocks, } from "@lunatest/runtime-intercept";
|
|
5
|
+
function toPrettyJson(input) {
|
|
6
|
+
return JSON.stringify(input, null, 2);
|
|
7
|
+
}
|
|
8
|
+
export function LunaDevtoolsPanel(props) {
|
|
9
|
+
const initialLuaScript = useMemo(() => props.initialLua ??
|
|
10
|
+
`scenario {
|
|
11
|
+
name = "swap-warning",
|
|
12
|
+
mode = "strict",
|
|
13
|
+
given = {
|
|
14
|
+
pool = { pair = "ETH/USDC", reserve0 = 100, reserve1 = 183000 },
|
|
15
|
+
wallet = { connected = true, ETH = 10.0 },
|
|
16
|
+
},
|
|
17
|
+
when = { action = "swap", input = { tokenIn = "ETH", amount = 50.0 } },
|
|
18
|
+
then_ui = { slippage_warning = true, severity = "high", button_disabled = true },
|
|
19
|
+
}`, [props.initialLua]);
|
|
20
|
+
const defaultRoutes = useMemo(() => props.initialRoutes ?? [
|
|
21
|
+
{
|
|
22
|
+
endpointType: "ethereum",
|
|
23
|
+
method: "eth_chainId",
|
|
24
|
+
responseKey: "chain-id",
|
|
25
|
+
},
|
|
26
|
+
], [props.initialRoutes]);
|
|
27
|
+
const defaultState = useMemo(() => props.initialState ?? {
|
|
28
|
+
chain: { id: 1, gasPrice: 30 },
|
|
29
|
+
}, [props.initialState]);
|
|
30
|
+
const [luaScript, setLuaScript] = useState(initialLuaScript);
|
|
31
|
+
const [routeJson, setRouteJson] = useState(toPrettyJson(defaultRoutes));
|
|
32
|
+
const [stateJson, setStateJson] = useState(toPrettyJson(defaultState));
|
|
33
|
+
const [status, setStatus] = useState("idle");
|
|
34
|
+
const [error, setError] = useState(null);
|
|
35
|
+
const [diff, setDiff] = useState(null);
|
|
36
|
+
const handleRun = async () => {
|
|
37
|
+
setError(null);
|
|
38
|
+
setDiff(null);
|
|
39
|
+
setStatus("running");
|
|
40
|
+
try {
|
|
41
|
+
const executed = props.onRunScenario
|
|
42
|
+
? await props.onRunScenario(luaScript)
|
|
43
|
+
: await (async () => {
|
|
44
|
+
const config = await loadLunaConfig(luaScript);
|
|
45
|
+
const result = await executeLuaScenario({
|
|
46
|
+
source: config,
|
|
47
|
+
adapter: {
|
|
48
|
+
runWhen: async ({ config: nextConfig, runtime }) => {
|
|
49
|
+
if (nextConfig.intercept?.routes) {
|
|
50
|
+
runtime.setRouteMocks(nextConfig.intercept.routes);
|
|
51
|
+
setRouteMocks(nextConfig.intercept.routes);
|
|
52
|
+
}
|
|
53
|
+
if (nextConfig.given) {
|
|
54
|
+
runtime.applyInterceptState(nextConfig.given);
|
|
55
|
+
applyInterceptState(nextConfig.given);
|
|
56
|
+
}
|
|
57
|
+
if (nextConfig.intercept?.state) {
|
|
58
|
+
runtime.applyInterceptState(nextConfig.intercept.state);
|
|
59
|
+
applyInterceptState(nextConfig.intercept.state);
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
resolveUi: async () => getInterceptState(),
|
|
63
|
+
resolveState: async () => getInterceptState(),
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
pass: result.pass,
|
|
68
|
+
error: result.error,
|
|
69
|
+
diff: result.result?.diff,
|
|
70
|
+
};
|
|
71
|
+
})();
|
|
72
|
+
setStatus(executed.pass ? "pass" : "fail");
|
|
73
|
+
if (executed.error) {
|
|
74
|
+
setError(executed.error);
|
|
75
|
+
}
|
|
76
|
+
if (executed.diff) {
|
|
77
|
+
setDiff(executed.diff);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (cause) {
|
|
81
|
+
setStatus("failed");
|
|
82
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const handleApplyRoutes = async () => {
|
|
86
|
+
setError(null);
|
|
87
|
+
setDiff(null);
|
|
88
|
+
setStatus("updating-routes");
|
|
89
|
+
try {
|
|
90
|
+
const parsed = JSON.parse(routeJson);
|
|
91
|
+
if (props.onSetRouteMocks) {
|
|
92
|
+
await props.onSetRouteMocks(parsed);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
setRouteMocks(parsed);
|
|
96
|
+
}
|
|
97
|
+
setStatus("routes-updated");
|
|
98
|
+
}
|
|
99
|
+
catch (cause) {
|
|
100
|
+
setStatus("failed");
|
|
101
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const handlePatchState = async () => {
|
|
105
|
+
setError(null);
|
|
106
|
+
setDiff(null);
|
|
107
|
+
setStatus("patching-state");
|
|
108
|
+
try {
|
|
109
|
+
const parsed = JSON.parse(stateJson);
|
|
110
|
+
if (props.onPatchState) {
|
|
111
|
+
await props.onPatchState(parsed);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
applyInterceptState(parsed);
|
|
115
|
+
}
|
|
116
|
+
setStatus("state-patched");
|
|
117
|
+
}
|
|
118
|
+
catch (cause) {
|
|
119
|
+
setStatus("failed");
|
|
120
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const handleReset = () => {
|
|
124
|
+
setLuaScript(initialLuaScript);
|
|
125
|
+
setRouteJson(toPrettyJson(defaultRoutes));
|
|
126
|
+
setStateJson(toPrettyJson(defaultState));
|
|
127
|
+
setStatus("reset");
|
|
128
|
+
setError(null);
|
|
129
|
+
setDiff(null);
|
|
130
|
+
};
|
|
131
|
+
return (_jsxs("aside", { "data-lunatest-devtools": true, style: {
|
|
132
|
+
position: "fixed",
|
|
133
|
+
right: 16,
|
|
134
|
+
bottom: 16,
|
|
135
|
+
width: 380,
|
|
136
|
+
maxHeight: "80vh",
|
|
137
|
+
overflow: "auto",
|
|
138
|
+
zIndex: 99999,
|
|
139
|
+
border: "1px solid #cbd5e1",
|
|
140
|
+
borderRadius: 12,
|
|
141
|
+
background: "#ffffff",
|
|
142
|
+
boxShadow: "0 12px 30px rgba(15, 23, 42, 0.18)",
|
|
143
|
+
fontFamily: "monospace",
|
|
144
|
+
fontSize: 12,
|
|
145
|
+
color: "#0f172a",
|
|
146
|
+
padding: 12,
|
|
147
|
+
}, children: [_jsx("h3", { style: { margin: 0, marginBottom: 8 }, children: props.title ?? "LunaTest Devtools" }), _jsx("p", { style: { margin: 0, marginBottom: 10, color: "#334155" }, children: "Lua DSL\uACFC \uC778\uD130\uC149\uD2B8 \uC0C1\uD0DC\uB97C \uBE0C\uB77C\uC6B0\uC800\uC5D0\uC11C \uBC14\uB85C \uC218\uC815\uD569\uB2C8\uB2E4." }), _jsx("label", { htmlFor: "lunatest-lua-script", children: "Lua Scenario" }), _jsx("textarea", { id: "lunatest-lua-script", value: luaScript, onChange: (event) => setLuaScript(event.currentTarget.value), style: { width: "100%", minHeight: 140, marginBottom: 8 } }), _jsx("button", { type: "button", onClick: handleRun, children: "Run Scenario" }), _jsx("hr", {}), _jsx("label", { htmlFor: "lunatest-routes", children: "Route Mocks (JSON array)" }), _jsx("textarea", { id: "lunatest-routes", value: routeJson, onChange: (event) => setRouteJson(event.currentTarget.value), style: { width: "100%", minHeight: 110, marginBottom: 8 } }), _jsx("button", { type: "button", onClick: handleApplyRoutes, children: "Apply Routes" }), _jsx("hr", {}), _jsx("label", { htmlFor: "lunatest-state", children: "Intercept State Patch (JSON object)" }), _jsx("textarea", { id: "lunatest-state", value: stateJson, onChange: (event) => setStateJson(event.currentTarget.value), style: { width: "100%", minHeight: 110, marginBottom: 8 } }), _jsx("button", { type: "button", onClick: handlePatchState, children: "Patch State" }), _jsx("button", { type: "button", onClick: handleReset, style: { marginLeft: 8 }, children: "Reset" }), _jsxs("p", { style: { marginTop: 10, marginBottom: 0 }, children: ["status: ", status] }), error ? (_jsxs("p", { style: { marginTop: 4, marginBottom: 0, color: "#b91c1c" }, children: ["error: ", error] })) : null, diff ? (_jsxs("pre", { style: { marginTop: 8, marginBottom: 0, color: "#7f1d1d", whiteSpace: "pre-wrap" }, children: ["diff: ", diff] })) : null] }));
|
|
148
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { LunaDevtoolsPanel } from "./LunaDevtoolsPanel.js";
|
|
3
|
+
export type MountLunaDevtoolsOptions = {
|
|
4
|
+
targetId?: string;
|
|
5
|
+
nodeEnv?: string;
|
|
6
|
+
panelProps?: React.ComponentProps<typeof LunaDevtoolsPanel>;
|
|
7
|
+
};
|
|
8
|
+
export declare function mountLunaDevtools(options?: MountLunaDevtoolsOptions): (() => void) | null;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { createRoot } from "react-dom/client";
|
|
3
|
+
import { LunaDevtoolsPanel } from "./LunaDevtoolsPanel.js";
|
|
4
|
+
import { resolveNodeEnv } from "../node-env.js";
|
|
5
|
+
const DEFAULT_TARGET_ID = "lunatest-devtools-root";
|
|
6
|
+
let activeRoot = null;
|
|
7
|
+
let activeTarget = null;
|
|
8
|
+
export function mountLunaDevtools(options = {}) {
|
|
9
|
+
if (typeof document === "undefined") {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
const nodeEnv = resolveNodeEnv(options.nodeEnv);
|
|
13
|
+
if (nodeEnv !== "development") {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const targetId = options.targetId ?? DEFAULT_TARGET_ID;
|
|
17
|
+
const existing = document.getElementById(targetId);
|
|
18
|
+
const target = existing ?? document.createElement("div");
|
|
19
|
+
if (!existing) {
|
|
20
|
+
target.id = targetId;
|
|
21
|
+
document.body.appendChild(target);
|
|
22
|
+
}
|
|
23
|
+
if (activeRoot) {
|
|
24
|
+
activeRoot.unmount();
|
|
25
|
+
activeRoot = null;
|
|
26
|
+
}
|
|
27
|
+
activeRoot = createRoot(target);
|
|
28
|
+
activeTarget = target;
|
|
29
|
+
activeRoot.render(React.createElement(LunaDevtoolsPanel, options.panelProps));
|
|
30
|
+
return () => {
|
|
31
|
+
if (!activeRoot || !activeTarget) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
activeRoot.unmount();
|
|
35
|
+
if (activeTarget.id === DEFAULT_TARGET_ID) {
|
|
36
|
+
activeTarget.remove();
|
|
37
|
+
}
|
|
38
|
+
activeRoot = null;
|
|
39
|
+
activeTarget = null;
|
|
40
|
+
};
|
|
41
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { createLunaProvider } from "./luna-provider.js";
|
|
2
|
+
export { LunaTestProvider } from "./LunaTestProvider.js";
|
|
3
|
+
export { useLunaProvider } from "./hooks/useLunaProvider.js";
|
|
4
|
+
export { useLunaTest } from "./useLunaTest.js";
|
|
5
|
+
export { withLunaWagmiConfig } from "./adapters/wagmi.js";
|
|
6
|
+
export { createEthersAdapter } from "./adapters/ethers.js";
|
|
7
|
+
export { createWeb3JsAdapter } from "./adapters/web3js.js";
|
|
8
|
+
export { enableLunaIntercept } from "./intercept.js";
|
|
9
|
+
export type { LunaBootstrapOptions, LunaBootstrapResult } from "./bootstrap.js";
|
|
10
|
+
export { bootstrapLunaRuntime } from "./bootstrap.js";
|
|
11
|
+
export { LunaDevtoolsPanel } from "./devtools/LunaDevtoolsPanel.js";
|
|
12
|
+
export { mountLunaDevtools } from "./devtools/mount.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { createLunaProvider } from "./luna-provider.js";
|
|
2
|
+
export { LunaTestProvider } from "./LunaTestProvider.js";
|
|
3
|
+
export { useLunaProvider } from "./hooks/useLunaProvider.js";
|
|
4
|
+
export { useLunaTest } from "./useLunaTest.js";
|
|
5
|
+
export { withLunaWagmiConfig } from "./adapters/wagmi.js";
|
|
6
|
+
export { createEthersAdapter } from "./adapters/ethers.js";
|
|
7
|
+
export { createWeb3JsAdapter } from "./adapters/web3js.js";
|
|
8
|
+
export { enableLunaIntercept } from "./intercept.js";
|
|
9
|
+
export { bootstrapLunaRuntime } from "./bootstrap.js";
|
|
10
|
+
export { LunaDevtoolsPanel } from "./devtools/LunaDevtoolsPanel.js";
|
|
11
|
+
export { mountLunaDevtools } from "./devtools/mount.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type LunaRuntimeInterceptConfig } from "@lunatest/runtime-intercept";
|
|
2
|
+
export type EnableLunaInterceptOptions = {
|
|
3
|
+
config?: LunaRuntimeInterceptConfig;
|
|
4
|
+
nodeEnv?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function enableLunaIntercept(options?: EnableLunaInterceptOptions): boolean;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { enableLunaRuntimeIntercept, } from "@lunatest/runtime-intercept";
|
|
2
|
+
import { resolveNodeEnv } from "./node-env.js";
|
|
3
|
+
export function enableLunaIntercept(options = {}) {
|
|
4
|
+
const nodeEnv = resolveNodeEnv(options.nodeEnv);
|
|
5
|
+
return enableLunaRuntimeIntercept(options.config ?? {}, nodeEnv);
|
|
6
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function resolveNodeEnv(explicit?: string): string | undefined;
|
package/dist/node-env.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function resolveNodeEnv(explicit) {
|
|
2
|
+
if (explicit) {
|
|
3
|
+
return explicit;
|
|
4
|
+
}
|
|
5
|
+
const modeFromImportMeta = import.meta?.env?.MODE;
|
|
6
|
+
if (typeof modeFromImportMeta === "string" && modeFromImportMeta.length > 0) {
|
|
7
|
+
return modeFromImportMeta;
|
|
8
|
+
}
|
|
9
|
+
if (typeof process !== "undefined") {
|
|
10
|
+
return process.env.NODE_ENV;
|
|
11
|
+
}
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useLunaTest(): import("./LunaTestProvider.js").LunaTestContextValue;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { LunaTestContext } from "./LunaTestProvider.js";
|
|
3
|
+
export function useLunaTest() {
|
|
4
|
+
const context = useContext(LunaTestContext);
|
|
5
|
+
if (!context) {
|
|
6
|
+
throw new Error("useLunaTest must be used within LunaTestProvider");
|
|
7
|
+
}
|
|
8
|
+
return context;
|
|
9
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lunatest/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public",
|
|
18
|
+
"tag": "latest"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@lunatest/core": "0.1.0",
|
|
25
|
+
"@lunatest/runtime-intercept": "0.1.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": "^18.3.1 || ^19.0.0",
|
|
29
|
+
"react-dom": "^18.3.1 || ^19.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"react": "^18.3.1",
|
|
33
|
+
"react-dom": "^18.3.1",
|
|
34
|
+
"@types/react": "^18.3.12",
|
|
35
|
+
"@types/react-dom": "^18.3.1"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc -p tsconfig.json",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"lint": "tsc -p tsconfig.json --noEmit"
|
|
41
|
+
}
|
|
42
|
+
}
|