| 4 | import vm from "node:vm"; |
| 5 | |
| 6 | async function loadDatasetModule() { |
| 7 | const source = await readFile(new URL("./dataset.js", import.meta.url), "utf8"); |
| 8 | const toolkit = await import("@reduxjs/toolkit"); |
| 9 | const context = vm.createContext({ |
| 10 | Headers, |
| 11 | fetch, |
| 12 | }); |
| 13 | |
| 14 | const modules = { |
| 15 | "@reduxjs/toolkit": new vm.SyntheticModule(["createSlice", "createAsyncThunk"], function() { |
| 16 | this.setExport("createSlice", toolkit.createSlice); |
| 17 | this.setExport("createAsyncThunk", toolkit.createAsyncThunk); |
| 18 | }, { context }), |
| 19 | "../modules/auth": new vm.SyntheticModule(["getAuthToken"], function() { |
| 20 | this.setExport("getAuthToken", () => "token"); |
| 21 | }, { context }), |
| 22 | "../config/settings": new vm.SyntheticModule(["API_HOST"], function() { |
| 23 | this.setExport("API_HOST", "http://localhost"); |
| 24 | }, { context }), |
| 25 | }; |
| 26 | |
| 27 | const mod = new vm.SourceTextModule(source, { |
| 28 | context, |
| 29 | identifier: "dataset.js", |
| 30 | }); |
| 31 | |
| 32 | await mod.link((specifier) => modules[specifier]); |
| 33 | await mod.evaluate(); |
| 34 | |
| 35 | return mod.namespace; |
| 36 | } |
| 37 | |
| 38 | test("dataset selectors hide data that belongs to a different active team", async () => { |
| 39 | const { selectDatasets, selectDatasetsNoDrafts } = await loadDatasetModule(); |