{"version":3,"file":"environment.mjs","names":[],"sources":["../../src/vitest/environment.ts"],"sourcesContent":["import { ES_BUILTINS } from \"#/utils/es-builtins\";\n// Import from ./globals (NOT ./mock): this module runs in the Vitest environment\n// realm where `vi` is unavailable, and ./mock imports `vi`. ./globals is\n// dependency-free of vitest.\nimport { RUNTIME_FLAG_KEY, cleanupPlatformGlobals, installPlatformGlobals } from \"./globals\";\n\n// Globals allowed in the Tailor Platform runtime.\n// ES_BUILTINS is the free-variable allowlist the deploy-time check uses, so\n// the emulated runtime exposes exactly the same identifiers as the production\n// platform (ECMAScript builtins + shared Node/browser runtime globals like\n// console, fetch, setTimeout).\n// Platform API mocks (tailor, tailordb, etc.) are not listed here — the base\n// surface is injected by installPlatformGlobals() after the whitelist cleanup,\n// and per-namespace mocks are layered on at `using xMock()` acquisition time.\nconst ALLOWED_GLOBALS = new Set([\n  ...ES_BUILTINS,\n\n  // The environment-active flag (set in installPlatformGlobals, used by\n  // setup.ts to detect the tailor-runtime environment).\n  RUNTIME_FLAG_KEY,\n\n  // Used by Vitest internally — not in the platform runtime, but removing breaks the test runner\n  \"process\",\n  \"require\",\n  \"module\",\n  \"exports\",\n  \"__vitest_worker__\",\n  \"__vitest_mocker__\",\n  \"VITEST_POOL_ID\",\n]);\n\nexport default {\n  name: \"tailor-runtime\",\n  viteEnvironment: \"ssr\",\n\n  async setup(global: typeof globalThis) {\n    const g = global as Record<string, unknown>;\n\n    const temporalDescriptor = Object.getOwnPropertyDescriptor(g, \"Temporal\");\n    const needsTemporal = g.Temporal === undefined;\n    const temporal = needsTemporal\n      ? (await import(\"temporal-polyfill/full/implementation\")).Temporal\n      : undefined;\n\n    // Save and remove all non-whitelisted globals\n    const saved: Record<string, PropertyDescriptor> = {};\n    for (const key of Object.getOwnPropertyNames(g)) {\n      if (!ALLOWED_GLOBALS.has(key)) {\n        const descriptor = Object.getOwnPropertyDescriptor(g, key);\n        if (descriptor?.configurable) {\n          saved[key] = descriptor;\n          delete g[key];\n        }\n      }\n    }\n\n    // Install the base platform surface after whitelist cleanup. Per-namespace\n    // mocks (workflow, tailordb, …) are installed on demand by `using xMock()`.\n    installPlatformGlobals(global);\n    if (needsTemporal) {\n      Object.defineProperty(g, \"Temporal\", {\n        value: temporal,\n        writable: true,\n        configurable: true,\n      });\n    }\n\n    return {\n      teardown(global: typeof globalThis) {\n        cleanupPlatformGlobals(global);\n\n        if (needsTemporal) {\n          if (temporalDescriptor) {\n            Object.defineProperty(global, \"Temporal\", temporalDescriptor);\n          } else {\n            Reflect.deleteProperty(global, \"Temporal\");\n          }\n        }\n\n        // Restore removed globals\n        const g = global as Record<string, unknown>;\n        for (const [key, descriptor] of Object.entries(saved)) {\n          Object.defineProperty(g, key, descriptor);\n        }\n      },\n    };\n  },\n};\n"],"mappings":"0GAcA,MAAM,EAAkB,IAAI,IAAI,CAC9B,GAAG,EAIH,EAGA,UACA,UACA,SACA,UACA,oBACA,oBACA,gBACF,CAAC,EAED,IAAA,EAAe,CACb,KAAM,iBACN,gBAAiB,MAEjB,MAAM,MAAM,EAA2B,CACrC,IAAM,EAAI,EAEJ,EAAqB,OAAO,yBAAyB,EAAG,UAAU,EAClE,EAAgB,EAAE,WAAa,IAAA,GAC/B,EAAW,GACZ,MAAM,OAAO,yCAAA,CAA0C,SACxD,IAAA,GAGE,EAA4C,CAAC,EACnD,IAAK,IAAM,KAAO,OAAO,oBAAoB,CAAC,EAC5C,GAAI,CAAC,EAAgB,IAAI,CAAG,EAAG,CAC7B,IAAM,EAAa,OAAO,yBAAyB,EAAG,CAAG,EACrD,GAAY,eACd,EAAM,GAAO,EACb,OAAO,EAAE,GAEb,CAcF,OATA,EAAuB,CAAM,EACzB,GACF,OAAO,eAAe,EAAG,WAAY,CACnC,MAAO,EACP,SAAU,GACV,aAAc,EAChB,CAAC,EAGI,CACL,SAAS,EAA2B,CAClC,EAAuB,CAAM,EAEzB,IACE,EACF,OAAO,eAAe,EAAQ,WAAY,CAAkB,EAE5D,QAAQ,eAAe,EAAQ,UAAU,GAK7C,IAAM,EAAI,EACV,IAAK,GAAM,CAAC,EAAK,KAAe,OAAO,QAAQ,CAAK,EAClD,OAAO,eAAe,EAAG,EAAK,CAAU,CAE5C,CACF,CACF,CACF"}