Skip to content

Commit

Permalink
Make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzal3x committed Oct 21, 2024
1 parent dca8da8 commit 75b90e1
Showing 1 changed file with 50 additions and 35 deletions.
85 changes: 50 additions & 35 deletions portal/common/lib/resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import { SuiClient } from "@mysten/sui/client";
import { HttpStatusCodes } from "./http/http_status_codes";
import { checkRedirect } from "./redirects";
import { fromBase64 } from "@mysten/bcs";
import { DynamicFieldStruct } from "./bcs_data_parsing";

// Mock SuiClient methods
const multiGetObjects = vi.fn();
const mockClient = {
multiGetObjects,
} as unknown as SuiClient;

vi.mock("@mysten/sui/utils", () => ({
deriveDynamicFieldID: vi.fn(() => "0xdynamicFieldId"),
}))

// Mock checkRedirect
vi.mock("./redirects", () => ({
checkRedirect: vi.fn(),
Expand Down Expand Up @@ -84,48 +87,60 @@ describe("fetchResource", () => {
const result = await fetchResource(mockClient, "0x1", "/path", new Set());
expect(result).toEqual({
blob_id: "0xresourceBlobId",
objectId: "0x3cf9bff169db6f780a0a3cae7b3b770097c26342ad0c08604bc80728cfa37bdc",
objectId: "0xdynamicFieldId",
version: undefined,
});
expect(checkRedirect).toHaveBeenCalledTimes(1);
});

// test("should follow redirect and recursively fetch resource", async () => {
// // Mock the redirect check to return a redirect ID on the first call
// (checkRedirect as any).mockResolvedValueOnce(
// "0x51813e7d4040265af8bd6c757f52accbe11e6df5b9cf3d6696a96e3f54fad096",
// );
// (checkRedirect as any).mockResolvedValueOnce(undefined);
test("should follow redirect and recursively fetch resource", async () => {
const mockObject = {
"objectId": "0x26dc2460093a9d6d31b58cb0ed1e72b19d140542a49be7472a6f25d542cb5cc3",
"version": "150835605",
"digest": "DDD7ZZvLkBQjq1kJpRsPDMpqhvYtGM878SdCTfF42ywE",
"display": {
"data": {
"walrus site address": "0x2"
},
"error": null
},
"bcs": {
"dataType": "moveObject",
"type": "0x1::flatland::Flatlander",
"hasPublicTransfer": true,
"version": 150835605,
}
};

const mockResource = {
data: {
bcs: {
dataType: "moveObject",
bcsBytes: "mockBcsBytes",
},
},
};

// // Mock the first resource object response
// getObject.mockResolvedValueOnce({
// data: {
// bcs: {
// dataType: "moveObject",
// bcsBytes: "mockBcsBytes",
// },
// },
// });
(checkRedirect as any).mockResolvedValueOnce(undefined);

// // Mock the final resource object response
// getObject.mockResolvedValueOnce({
// data: {
// bcs: {
// dataType: "moveObject",
// bcsBytes: "mockBcsBytes",
// },
// },
// });
multiGetObjects
.mockResolvedValueOnce([mockObject, mockResource])
.mockResolvedValueOnce([mockObject, mockResource]);

// const result = await fetchResource(mockClient, "0x1", "/path", new Set());
const result = await fetchResource(
mockClient,
"0x26dc2460093a9d6d31b58cb0ed1e72b19d140542a49be7472a6f25d542cb5cc3",
"/path",
new Set());

// // Verify the results
// expect(result).toEqual({
// blob_id: "0xresourceBlobId",
// objectId: "0x3cf9bff169db6f780a0a3cae7b3b770097c26342ad0c08604bc80728cfa37bdc",
// version: undefined,
// });
// expect(checkRedirect).toHaveBeenCalledTimes(1);
// });
// Verify the results
expect(result).toEqual({
blob_id: "0xresourceBlobId",
objectId: "0xdynamicFieldId",
version: undefined,
});
expect(checkRedirect).toHaveBeenCalledTimes(2);
});

// test("should return NOT_FOUND if the resource does not contain a blob_id", async () => {
// const seenResources = new Set<string>();
Expand Down

0 comments on commit 75b90e1

Please sign in to comment.