Skip to content

Commit

Permalink
fix: Device.toJSON() properties (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Sep 3, 2024
1 parent 826587b commit c4182d3
Show file tree
Hide file tree
Showing 6 changed files with 610 additions and 303 deletions.
7 changes: 5 additions & 2 deletions integration-tests/js-compute/compare-downstream-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export async function compareDownstreamResponse (configResponse, actualResponse)
let errors = [];
// Status
if (configResponse.status != actualResponse.statusCode) {
errors.push(new Error(`[DownstreamResponse: Status mismatch] Expected: ${configResponse.status} - Got: ${actualResponse.statusCode}`));
let bodySummary = '';
try {
bodySummary = (await actualResponse.body.text()).slice(0, 1000);
} catch {}
errors.push(new Error(`[DownstreamResponse: Status mismatch] Expected: ${configResponse.status} - Got: ${actualResponse.statusCode}\n${bodySummary}`));
}

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

// Body
if (configResponse.body) {

// Check if we need to stream the response and check the chunks, or the whole body
if (configResponse.body instanceof Array) {
// Stream down the response
Expand Down
189 changes: 189 additions & 0 deletions integration-tests/js-compute/fixtures/app/src/assertions-throwing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Testing/Assertion functions //

export async function sleep(milliseconds) {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
}

// TODO: Implement ReadableStream getIterator() and [@@asyncIterator]() methods
export async function streamToString(stream) {
const decoder = new TextDecoder();
let string = "";
let reader = stream.getReader();
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) {
return string;
}
string += decoder.decode(value);
}
}

export function iteratableToStream(iterable) {
return new ReadableStream({
async pull(controller) {
for await (const value of iterable) {
controller.enqueue(value);
}
controller.close();
},
});
}

export function pass(message = "") {
return new Response(message);
}

export function fail(message = "") {
throw new Response(message, { status: 500 });
}

function prettyPrintSymbol(a) {
if (typeof a === "symbol") {
return String(a);
}
return a;
}
export function assert(actual, expected, code) {
if (!deepEqual(actual, expected)) {
fail(
`Expected \`${code}\` to equal \`${JSON.stringify(prettyPrintSymbol(expected))}\` - Found \`${JSON.stringify(prettyPrintSymbol(actual))}\``
);
}
}

export async function assertResolves(func) {
try {
await func();
} catch (error) {
fail(
`Expected \`${func.toString()}\` to resolve - Found it rejected: ${error.name}: ${error.message}`
);
}
}

export async function assertRejects(func, errorClass, errorMessage) {
try {
await func();
} catch (error) {
if (errorClass) {
if (error instanceof errorClass === false) {
fail(
`Expected \`${func.toString()}\` to reject instance of \`${errorClass.name}\` - Found instance of \`${error.name}\``
);
}
}

if (errorMessage) {
if (error.message !== errorMessage) {
fail(
`Expected \`${func.toString()}\` to reject error message of \`${errorMessage}\` - Found \`${error.message}\``
);
}
}

return;
}
fail(
`Expected \`${func.toString()}\` to reject - Found it did not reject`
);
}

export function assertThrows(func, errorClass, errorMessage) {
try {
func();
} catch (error) {
if (errorClass) {
if (error instanceof errorClass === false) {
fail(
`Expected \`${func.toString()}\` to throw instance of \`${errorClass.name}\` - Found instance of \`${error.name}\`: ${error.message}\n${error.stack}`
);
}
}

if (errorMessage) {
if (error.message !== errorMessage) {
fail(
`Expected \`${func.toString()}\` to throw error message of \`${errorMessage}\` - Found \`${error.message}\``
);
}
}

return;
}
fail(
`Expected \`${func.toString()}\` to throw - Found it did not throw`
);
}

export function assertDoesNotThrow(func) {
try {
func();
} catch (error) {
fail(
`Expected \`${func.toString()}\` to not throw - Found it did throw: ${error.name}: ${error.message}`
);
}
}

export function deepEqual(a, b) {
var aKeys;
var bKeys;
var typeA;
var typeB;
var key;
var i;

typeA = typeof a;
typeB = typeof b;
if (a === null || typeA !== "object") {
if (b === null || typeB !== "object") {
return a === b;
}
return false;
}

// Case: `a` is of type 'object'
if (b === null || typeB !== "object") {
return false;
}
if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) {
return false;
}
if (a instanceof Date) {
return a.getTime() === b.getTime();
}
if (a instanceof RegExp) {
return a.source === b.source && a.flags === b.flags;
}
if (a instanceof Error) {
if (a.message !== b.message || a.name !== b.name) {
return false;
}
}

aKeys = Object.keys(a);
bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
return false;
}
aKeys.sort();
bKeys.sort();

// Cheap key test:
for (i = 0; i < aKeys.length; i++) {
if (aKeys[i] !== bKeys[i]) {
return false;
}
}
// Possibly expensive deep equality test for each corresponding key:
for (i = 0; i < aKeys.length; i++) {
key = aKeys[i];
if (!deepEqual(a[key], b[key])) {
return false;
}
}
return typeA === typeB;
}
Loading

0 comments on commit c4182d3

Please sign in to comment.