Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Prateek Kumar <[email protected]>
  • Loading branch information
prateek-kumar-improving committed Sep 19, 2024
1 parent b719dfd commit 3aac146
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion node/tests/GlideClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
it,
} from "@jest/globals";
import {
convertGlideRecordToRecord,
Decoder,
FlushMode,
FunctionRestorePolicy,
Expand Down Expand Up @@ -46,6 +45,56 @@ import {
waitForScriptNotBusy,
} from "./TestUtilities";

/**
* @internal
* Check whether an object is a `GlideRecord` (see {@link GlideRecord}).
*/
export function isGlideRecord(obj?: unknown): boolean {
return (
Array.isArray(obj) &&
obj.length > 0 &&
typeof obj[0] === "object" &&
"key" in obj[0] &&
"value" in obj[0]
);
}

/**
* @internal
* Check whether an object is a `GlideRecord[]` (see {@link GlideRecord}).
*/
function isGlideRecordArray(obj?: unknown): boolean {
return Array.isArray(obj) && obj.length > 0 && isGlideRecord(obj[0]);
}

/**
* @internal
* Recursively downcast `GlideRecord` to `Record`. Use if `data` keys are always strings.
*/
export function convertGlideRecordToRecord<T>(
data: GlideRecord<T>,
): Record<string, T> {
const res: Record<string, T> = {};

for (const pair of data) {
let newVal = pair.value;

if (isGlideRecord(pair.value)) {
newVal = convertGlideRecordToRecord(
pair.value as GlideRecord<unknown>,
) as T;
} else if (isGlideRecordArray(pair.value)) {
newVal = (pair.value as GlideRecord<unknown>[]).map(
convertGlideRecordToRecord,
) as T;
}

res[pair.key as string] = newVal;
}

return res;
}

const TIMEOUT = 50000;

export declare class Script {
Expand Down

0 comments on commit 3aac146

Please sign in to comment.