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
3 changes: 3 additions & 0 deletions frontend/app/waveenv/waveenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export type BlockMetaKeyAtomFnType<Keys extends keyof MetaType = keyof MetaType>
export type WaveEnv = {
electron: ElectronApi;
rpc: RpcApiType;
platform: NodeJS.Platform;
configAtoms: ConfigAtoms;
isDev: () => boolean;
isWindows: () => boolean;
isMacOS: () => boolean;
atoms: GlobalAtomsType;
createBlock: (blockDef: BlockDef, magnified?: boolean, ephemeral?: boolean) => Promise<string>;
showContextMenu: (menu: ContextMenuItem[], e: React.MouseEvent) => void;
Expand Down
4 changes: 4 additions & 0 deletions frontend/app/waveenv/waveenvimpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "@/app/store/global";
import { RpcApi } from "@/app/store/wshclientapi";
import { WaveEnv } from "@/app/waveenv/waveenv";
import { isMacOS, isWindows, PLATFORM } from "@/util/platformutil";

const configAtoms = new Proxy({} as WaveEnv["configAtoms"], {
get<K extends keyof SettingsType>(_target: WaveEnv["configAtoms"], key: K) {
Expand All @@ -24,8 +25,11 @@ export function makeWaveEnvImpl(): WaveEnv {
return {
electron: (window as any).api,
rpc: RpcApi,
platform: PLATFORM,
configAtoms,
isDev,
isWindows,
isMacOS,
atoms,
createBlock,
showContextMenu: (menu: ContextMenuItem[], e: React.MouseEvent) => {
Expand Down
13 changes: 12 additions & 1 deletion frontend/preview/mock/mockwaveenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { getSettingsKeyAtom, makeDefaultConnStatus } from "@/app/store/global";
import { RpcApiType } from "@/app/store/wshclientapi";
import { WaveEnv } from "@/app/waveenv/waveenv";
import { PlatformMacOS, PlatformWindows } from "@/util/platformutil";
import { Atom, atom, PrimitiveAtom } from "jotai";
import { DefaultFullConfig } from "./defaultconfig";
import { previewElectronApi } from "./preview-electron-api";
Expand All @@ -14,6 +15,7 @@ type RpcOverrides = {

export type MockEnv = {
isDev?: boolean;
platform?: NodeJS.Platform;
settings?: Partial<SettingsType>;
rpc?: RpcOverrides;
atoms?: Partial<GlobalAtomsType>;
Expand All @@ -36,6 +38,7 @@ function mergeRecords<T>(base: Record<string, T>, overrides: Record<string, T>):
export function mergeMockEnv(base: MockEnv, overrides: MockEnv): MockEnv {
return {
isDev: overrides.isDev ?? base.isDev,
platform: overrides.platform ?? base.platform,
settings: mergeRecords(base.settings, overrides.settings),
rpc: mergeRecords(base.rpc as any, overrides.rpc as any) as RpcOverrides,
atoms: overrides.atoms != null || base.atoms != null ? { ...base.atoms, ...overrides.atoms } : undefined,
Expand Down Expand Up @@ -146,16 +149,24 @@ export function applyMockEnvOverrides(env: WaveEnv, newOverrides: MockEnv): Mock

export function makeMockWaveEnv(mockEnv?: MockEnv): MockWaveEnv {
const overrides: MockEnv = mockEnv ?? {};
const platform = overrides.platform ?? PlatformMacOS;
const connStatusAtomCache = new Map<string, PrimitiveAtom<ConnStatus>>();
const waveObjectAtomCache = new Map<string, PrimitiveAtom<WaveObj>>();
const blockMetaKeyAtomCache = new Map<string, Atom<any>>();
const env = {
mockEnv: overrides,
electron: overrides.electron ? { ...previewElectronApi, ...overrides.electron } : previewElectronApi,
electron: {
...previewElectronApi,
getPlatform: () => platform,
Copy link
Contributor

Choose a reason for hiding this comment

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

WARNING: The getPlatform function should be defined AFTER the ...overrides.electron spread to ensure it always returns the correct platform value, even when users override other electron properties.

Currently, if a user passes getPlatform in overrides.electron, it will override the platform-based implementation. The correct order should be:

electron: {
    ...previewElectronApi,
    ...overrides.electron,
    getPlatform: () => platform,
},

...overrides.electron,
},
rpc: makeMockRpc(overrides.rpc),
platform,
configAtoms: makeMockConfigAtoms(overrides.settings),
atoms: makeMockGlobalAtoms(overrides.settings, overrides.atoms),
isDev: () => overrides.isDev ?? true,
isWindows: () => platform === PlatformWindows,
isMacOS: () => platform === PlatformMacOS,
createBlock:
overrides.createBlock ??
((blockDef: BlockDef, magnified?: boolean, ephemeral?: boolean) => {
Expand Down
Loading