Skip to content

Commit c4182d3

Browse files
author
Guy Bedford
authored
fix: Device.toJSON() properties (#937)
1 parent 826587b commit c4182d3

File tree

6 files changed

+610
-303
lines changed

6 files changed

+610
-303
lines changed

integration-tests/js-compute/compare-downstream-response.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export async function compareDownstreamResponse (configResponse, actualResponse)
1616
let errors = [];
1717
// Status
1818
if (configResponse.status != actualResponse.statusCode) {
19-
errors.push(new Error(`[DownstreamResponse: Status mismatch] Expected: ${configResponse.status} - Got: ${actualResponse.statusCode}`));
19+
let bodySummary = '';
20+
try {
21+
bodySummary = (await actualResponse.body.text()).slice(0, 1000);
22+
} catch {}
23+
errors.push(new Error(`[DownstreamResponse: Status mismatch] Expected: ${configResponse.status} - Got: ${actualResponse.statusCode}\n${bodySummary}`));
2024
}
2125

2226
// Headers
@@ -26,7 +30,6 @@ export async function compareDownstreamResponse (configResponse, actualResponse)
2630

2731
// Body
2832
if (configResponse.body) {
29-
3033
// Check if we need to stream the response and check the chunks, or the whole body
3134
if (configResponse.body instanceof Array) {
3235
// Stream down the response
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// Testing/Assertion functions //
2+
3+
export async function sleep(milliseconds) {
4+
return new Promise((resolve) => {
5+
setTimeout(resolve, milliseconds);
6+
});
7+
}
8+
9+
// TODO: Implement ReadableStream getIterator() and [@@asyncIterator]() methods
10+
export async function streamToString(stream) {
11+
const decoder = new TextDecoder();
12+
let string = "";
13+
let reader = stream.getReader();
14+
// eslint-disable-next-line no-constant-condition
15+
while (true) {
16+
const { done, value } = await reader.read();
17+
if (done) {
18+
return string;
19+
}
20+
string += decoder.decode(value);
21+
}
22+
}
23+
24+
export function iteratableToStream(iterable) {
25+
return new ReadableStream({
26+
async pull(controller) {
27+
for await (const value of iterable) {
28+
controller.enqueue(value);
29+
}
30+
controller.close();
31+
},
32+
});
33+
}
34+
35+
export function pass(message = "") {
36+
return new Response(message);
37+
}
38+
39+
export function fail(message = "") {
40+
throw new Response(message, { status: 500 });
41+
}
42+
43+
function prettyPrintSymbol(a) {
44+
if (typeof a === "symbol") {
45+
return String(a);
46+
}
47+
return a;
48+
}
49+
export function assert(actual, expected, code) {
50+
if (!deepEqual(actual, expected)) {
51+
fail(
52+
`Expected \`${code}\` to equal \`${JSON.stringify(prettyPrintSymbol(expected))}\` - Found \`${JSON.stringify(prettyPrintSymbol(actual))}\``
53+
);
54+
}
55+
}
56+
57+
export async function assertResolves(func) {
58+
try {
59+
await func();
60+
} catch (error) {
61+
fail(
62+
`Expected \`${func.toString()}\` to resolve - Found it rejected: ${error.name}: ${error.message}`
63+
);
64+
}
65+
}
66+
67+
export async function assertRejects(func, errorClass, errorMessage) {
68+
try {
69+
await func();
70+
} catch (error) {
71+
if (errorClass) {
72+
if (error instanceof errorClass === false) {
73+
fail(
74+
`Expected \`${func.toString()}\` to reject instance of \`${errorClass.name}\` - Found instance of \`${error.name}\``
75+
);
76+
}
77+
}
78+
79+
if (errorMessage) {
80+
if (error.message !== errorMessage) {
81+
fail(
82+
`Expected \`${func.toString()}\` to reject error message of \`${errorMessage}\` - Found \`${error.message}\``
83+
);
84+
}
85+
}
86+
87+
return;
88+
}
89+
fail(
90+
`Expected \`${func.toString()}\` to reject - Found it did not reject`
91+
);
92+
}
93+
94+
export function assertThrows(func, errorClass, errorMessage) {
95+
try {
96+
func();
97+
} catch (error) {
98+
if (errorClass) {
99+
if (error instanceof errorClass === false) {
100+
fail(
101+
`Expected \`${func.toString()}\` to throw instance of \`${errorClass.name}\` - Found instance of \`${error.name}\`: ${error.message}\n${error.stack}`
102+
);
103+
}
104+
}
105+
106+
if (errorMessage) {
107+
if (error.message !== errorMessage) {
108+
fail(
109+
`Expected \`${func.toString()}\` to throw error message of \`${errorMessage}\` - Found \`${error.message}\``
110+
);
111+
}
112+
}
113+
114+
return;
115+
}
116+
fail(
117+
`Expected \`${func.toString()}\` to throw - Found it did not throw`
118+
);
119+
}
120+
121+
export function assertDoesNotThrow(func) {
122+
try {
123+
func();
124+
} catch (error) {
125+
fail(
126+
`Expected \`${func.toString()}\` to not throw - Found it did throw: ${error.name}: ${error.message}`
127+
);
128+
}
129+
}
130+
131+
export function deepEqual(a, b) {
132+
var aKeys;
133+
var bKeys;
134+
var typeA;
135+
var typeB;
136+
var key;
137+
var i;
138+
139+
typeA = typeof a;
140+
typeB = typeof b;
141+
if (a === null || typeA !== "object") {
142+
if (b === null || typeB !== "object") {
143+
return a === b;
144+
}
145+
return false;
146+
}
147+
148+
// Case: `a` is of type 'object'
149+
if (b === null || typeB !== "object") {
150+
return false;
151+
}
152+
if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) {
153+
return false;
154+
}
155+
if (a instanceof Date) {
156+
return a.getTime() === b.getTime();
157+
}
158+
if (a instanceof RegExp) {
159+
return a.source === b.source && a.flags === b.flags;
160+
}
161+
if (a instanceof Error) {
162+
if (a.message !== b.message || a.name !== b.name) {
163+
return false;
164+
}
165+
}
166+
167+
aKeys = Object.keys(a);
168+
bKeys = Object.keys(b);
169+
if (aKeys.length !== bKeys.length) {
170+
return false;
171+
}
172+
aKeys.sort();
173+
bKeys.sort();
174+
175+
// Cheap key test:
176+
for (i = 0; i < aKeys.length; i++) {
177+
if (aKeys[i] !== bKeys[i]) {
178+
return false;
179+
}
180+
}
181+
// Possibly expensive deep equality test for each corresponding key:
182+
for (i = 0; i < aKeys.length; i++) {
183+
key = aKeys[i];
184+
if (!deepEqual(a[key], b[key])) {
185+
return false;
186+
}
187+
}
188+
return typeA === typeB;
189+
}

0 commit comments

Comments
 (0)