Skip to content

Commit 09537f8

Browse files
authored
Update MSW handlers (#41)
1 parent 8b467b8 commit 09537f8

File tree

3 files changed

+130
-66
lines changed

3 files changed

+130
-66
lines changed

web-conexs-client/src/main.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import "./index.css";
44
import { BrowserRouter } from "react-router-dom";
55
import App from "./App.tsx";
66

7-
//import axios from "axios";
7+
import axios from "axios";
88

99
async function enableMocking() {
10-
//axios.defaults.headers.common["Authorization"] = "Bearer test_user4";
11-
return;
12-
// if (process.env.NODE_ENV !== "development") {
13-
// return;
14-
// }
15-
// const { worker } = await import("./mocks/browser");
16-
// // `worker.start()` returns a Promise that resolves
17-
// // once the Service Worker is up and ready to intercept requests.
18-
// return worker.start();
10+
if (process.env.NODE_ENV !== "development") {
11+
return;
12+
}
13+
14+
axios.defaults.headers.common["Authorization"] = "Bearer test_user";
15+
16+
const { worker } = await import("./mocks/browser");
17+
// `worker.start()` returns a Promise that resolves
18+
// once the Service Worker is up and ready to intercept requests.
19+
return worker.start();
1920
}
2021

2122
enableMocking().then(() => {
Lines changed: 118 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { http, HttpResponse, delay } from "msw";
22
import {
33
Crystal,
4+
CrystalInput,
45
HPCCluster,
56
Molecule,
67
MoleculeInput,
@@ -9,34 +10,85 @@ import {
910
Person,
1011
Simulation,
1112
SimulationPage,
13+
Structure,
14+
StructureWithMetadata,
15+
XASData,
1216
} from "../models";
17+
import { Cluster } from "cluster";
1318

1419
const orcaMockOutput = "********\n**ORCA RESULT**\n********";
20+
const orcaMockJobfile = "********\n**ORCA JOBFILE**\n********";
1521

16-
const mockMolecule: Molecule = {
22+
const mockCrystalStructure: Crystal = {
1723
id: 1,
18-
label: "MockBenzene",
19-
20-
structure:
21-
"C 0.000000 1.402720 0.000000\nH 0.000000 2.490290 0.000000\nC -1.214790 0.701360 0.000000\nH -2.156660 1.245150 0.000000\nC -1.214790 -0.701360 0.000000\nH -2.156660 -1.245150 0.000000\nC 0.000000 -1.402720 0.000000\nH 0.000000 -2.490290 0.000000\nC 1.214790 -0.701360 0.000000\nH 2.156660 -1.245150 0.000000\nC 1.214790 0.701360 0.000000\nH 2.156660 1.245150 0.000000",
24+
lattice: {
25+
a: 4.1043564,
26+
b: 4.1043564,
27+
c: 4.1043564,
28+
alpha: 90,
29+
beta: 90,
30+
gamma: 90,
31+
},
32+
label: "test",
33+
sites: [
34+
{ element_z: 47, x: 0.0, y: 0.0, z: 0.0, index: 1 },
35+
{ element_z: 47, x: 0.5, y: 0.5, z: 0.0, index: 2 },
36+
{ element_z: 47, x: 0.5, y: 0.0, z: 0.5, index: 3 },
37+
{ element_z: 47, x: 0.0, y: 0.5, z: 0.5, index: 4 },
38+
],
2239
};
2340

24-
const mockCrystal: Crystal = {
41+
const mockCrystal: Structure = {
2542
id: 1,
43+
label: "test",
44+
person_id: 1,
45+
lattice_id: 1,
46+
};
47+
48+
const mockCrysWithMetadata: StructureWithMetadata = {
49+
atom_count: 4,
50+
elements: [47],
51+
structure: mockCrystal,
52+
};
2653

27-
a: 4.1043564,
28-
b: 4.1043564,
29-
c: 4.1043564,
30-
alpha: 90,
31-
beta: 90,
32-
gamma: 90,
54+
const mockMoleculeStructure: Molecule = {
55+
id: 2,
56+
label: "Benzene",
57+
sites: [
58+
{ index: 1, element_z: 6, x: 0.0, y: 1.40272, z: 0.0 },
59+
{ index: 2, element_z: 1, x: 0.0, y: 2.49029, z: 0.0 },
60+
{ index: 3, element_z: 6, x: -1.21479, y: 0.70136, z: 0.0 },
61+
{ index: 4, element_z: 1, x: -2.15666, y: 1.24515, z: 0.0 },
62+
{ index: 5, element_z: 6, x: -1.21479, y: -0.70136, z: 0.0 },
63+
{ index: 6, element_z: 1, x: -2.15666, y: -1.24515, z: 0.0 },
64+
{ index: 7, element_z: 6, x: 0.0, y: -1.40272, z: 0.0 },
65+
{ index: 8, element_z: 1, x: 0.0, y: -2.49029, z: 0.0 },
66+
{ index: 9, element_z: 6, x: 1.21479, y: -0.70136, z: 0.0 },
67+
{ index: 10, element_z: 1, x: 2.15666, y: -1.24515, z: 0.0 },
68+
{ index: 11, element_z: 6, x: 1.21479, y: 0.70136, z: 0.0 },
69+
{ index: 12, element_z: 1, x: 2.15666, y: 1.24515, z: 0.0 },
70+
],
71+
};
72+
73+
const mockMole: Structure = {
74+
id: 2,
3375
label: "test",
34-
structure: "Ag 0. 0. 0.\nAg 0.5 0.5 0.\nAg 0.5 0. 0.5\nAg 0. 0.5 0.5",
76+
person_id: 1,
77+
lattice_id: null,
3578
};
3679

37-
const mockCrystalArray = [mockCrystal];
80+
const mockMoleWithMetadata: StructureWithMetadata = {
81+
atom_count: 12,
82+
elements: [1, 6],
83+
structure: mockMole,
84+
};
3885

39-
const mockArray = [mockMolecule];
86+
const mockXASdata: XASData = {
87+
energy: [1, 2, 3, 4, 5],
88+
xas: [1, 2, 3, 2, 1],
89+
stk_energy: [2, 3, 3, 3, 4],
90+
stk_xas: [0, 0, 3, 0, 0],
91+
};
4092

4193
const mockSimulation: Simulation = {
4294
id: 1,
@@ -46,7 +98,7 @@ const mockSimulation: Simulation = {
4698
status: "mock",
4799
working_directory: "/mock",
48100
request_date: "2025-02-27T09:22:41.035872",
49-
chemical_structure_id: 1,
101+
chemical_structure_id: 2,
50102
submission_date: null,
51103
completion_date: null,
52104
};
@@ -69,34 +121,6 @@ const mockOrcaSimulation: OrcaSimulation = {
69121
const mockSimulations = [mockSimulation];
70122

71123
export const handlers = [
72-
http.get("/api/molecules", async () => {
73-
await delay(10000);
74-
return HttpResponse.json(mockArray);
75-
}),
76-
77-
http.get("/api/molecules/:id", ({ params }) => {
78-
const { id } = params;
79-
80-
const mol = mockArray.find((el) => el.id == Number(id));
81-
82-
if (mol) {
83-
return HttpResponse.json(mol);
84-
} else {
85-
return new HttpResponse(null, { status: 404 });
86-
}
87-
}),
88-
89-
http.post("/api/molecules", async ({ request }) => {
90-
// Read the intercepted request body as JSON.
91-
const newMolecule = (await request.json()) as MoleculeInput;
92-
93-
const responseMolecule: Molecule = { ...newMolecule, id: 10 };
94-
95-
// Don't forget to declare a semantic "201 Created"
96-
// response and send back the newly created post!
97-
return HttpResponse.json(responseMolecule, { status: 201 });
98-
}),
99-
100124
http.get("/api/simulations", () => {
101125
const page: SimulationPage = {
102126
items: [mockSimulation],
@@ -128,12 +152,18 @@ export const handlers = [
128152
return HttpResponse.json(mockOrcaSimulation);
129153
}),
130154

131-
http.get("/api/orca/:id/output", ({ params }) => {
132-
const { id } = params;
133-
155+
http.get("/api/orca/:id/output", () => {
134156
return HttpResponse.text(orcaMockOutput);
135157
}),
136158

159+
http.get("/api/orca/:id/jobfile", () => {
160+
return HttpResponse.text(orcaMockJobfile);
161+
}),
162+
163+
http.get("/api/orca/:id/xas", () => {
164+
return HttpResponse.json(mockXASdata);
165+
}),
166+
137167
http.post("/api/submit/orca", async ({ request }) => {
138168
// Read the intercepted request body as JSON.
139169
const newOrcaSimulation = (await request.json()) as OrcaSimulationInput;
@@ -148,38 +178,71 @@ export const handlers = [
148178
return HttpResponse.json(responseSimulation, { status: 201 });
149179
}),
150180

151-
http.get("/api/crystals", async () => {
152-
return HttpResponse.json(mockCrystalArray);
153-
}),
154-
155181
http.get("/api/user", async (request) => {
156182
const auth = request.request.headers.get("authorization");
157183

158184
if (auth && auth.startsWith("Bearer ")) {
159-
const user: Person = { identifier: auth.slice(7) };
185+
const user: Person = {
186+
identifier: auth.slice(7),
187+
accepted_orca_eula: true,
188+
};
160189
return HttpResponse.json(user);
161190
}
162191

163192
return new HttpResponse(null, { status: 401 });
164193
}),
165194

195+
http.get("/api/cluster/status", async () => {
196+
const clusterStatus: HPCCluster = {
197+
id: 1,
198+
updated: new Date(Date.now()).toISOString().slice(0, -1),
199+
};
200+
return HttpResponse.json(clusterStatus);
201+
}),
202+
166203
http.get("/api/matproj/:id", async (request) => {
167204
const auth = request.request.headers.get("authorization");
168205

169206
if (auth && auth.startsWith("Bearer ")) {
170-
return HttpResponse.json(mockCrystal);
207+
return HttpResponse.json(mockCrystalStructure);
171208
}
172209

173210
return new HttpResponse(null, { status: 401 });
174211
}),
175212

176213
http.get("/api/cluster/status", async () => {
214+
const cluster: HPCCluster = { id: 1, updated: Date.now().toString() };
177215

216+
return HttpResponse.json(cluster);
217+
}),
178218

179-
const cluster : HPCCluster = {id: 1, updated: Date.now().toString()}
219+
http.get("/api/structures", async ({ request }) => {
220+
const url = new URL(request.url);
180221

222+
console.log(url);
181223

224+
// Given a request url of "/product?id=1",
225+
// the `productId` will be a "1" string.
226+
const type = url.searchParams.get("type");
227+
228+
console.log(type);
229+
230+
if (type == "crystal") {
231+
return HttpResponse.json([mockCrysWithMetadata]);
232+
} else {
233+
return HttpResponse.json([mockMoleWithMetadata]);
234+
}
235+
}),
236+
237+
http.get("/api/structures/:id", async ({ params }) => {
238+
const { id } = params;
239+
240+
if (id == "1") {
241+
return HttpResponse.json(mockCrystalStructure);
242+
} else if (id == "2") {
243+
return HttpResponse.json(mockMoleculeStructure);
244+
}
182245

183-
return HttpResponse.json(cluster)
246+
return new HttpResponse(null, { status: 404 });
184247
}),
185248
];

web-conexs-client/src/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface Structure {
4646
label: string;
4747
person_id: number;
4848
id: number;
49-
lattice_id: number;
49+
lattice_id: number | null;
5050
}
5151

5252
export interface StructureWithMetadata {

0 commit comments

Comments
 (0)