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 fb73702
Showing 1 changed file with 93 additions and 94 deletions.
187 changes: 93 additions & 94 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,101 +87,97 @@ 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 () => {
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",
},
},
};

(checkRedirect as any).mockResolvedValueOnce(undefined);

multiGetObjects
.mockResolvedValueOnce([mockObject, mockResource])
.mockResolvedValueOnce([mockObject, mockResource]);

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

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

// 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);

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

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

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

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

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

// (checkRedirect as any).mockResolvedValueOnce(
// "0x51813e7d4040265af8bd6c757f52accbe11e6df5b9cf3d6696a96e3f54fad096",
// );

// // Mock getObject to return a valid BCS object
// getObject.mockResolvedValueOnce({
// data: {
// bcs: {
// dataType: "moveObject",
// bcsBytes: "mockBcsBytes",
// },
// },
// });
// getObject.mockResolvedValueOnce({
// data: {
// bcs: {
// dataType: "moveObject",
// bcsBytes: "mockBcsBytes",
// },
// },
// });

// // Mock fromBase64 to simulate the decoding process
// (fromBase64 as any).mockReturnValueOnce("decodedBcsBytes");

// // Mock DynamicFieldStruct to return a resource without a blob_id
// (DynamicFieldStruct as any).mockImplementation(() => ({
// parse: () => ({ value: mockResource }),
// }));

// const result = await fetchResource(mockClient, "0x1", "/path", seenResources);

// // Since the resource does not have a blob_id, the function should return NOT_FOUND
// expect(result).toBe(HttpStatusCodes.NOT_FOUND);
// });

// test("should return NOT_FOUND if dynamic fields are not found", async () => {
// const seenResources = new Set<string>();

// // Mock to return no redirect
// (checkRedirect as any).mockResolvedValueOnce(null);

// // Mock to simulate that dynamic fields are not found
// getObject.mockResolvedValueOnce(undefined);

// const result = await fetchResource(mockClient, "0x1", "/path", seenResources);

// // Check that the function returns NOT_FOUND
// expect(result).toBe(HttpStatusCodes.NOT_FOUND);
// });
test("should return NOT_FOUND if the resource does not contain a blob_id", async () => {
const seenResources = new Set<string>();
(checkRedirect as any).mockResolvedValueOnce(undefined);
multiGetObjects.mockReturnValue([
{
data: {
bcs: {
dataType: "moveObject",
},
},
},
{},
]);
(fromBase64 as any).mockReturnValueOnce(undefined);

const result = await fetchResource(mockClient, "0x1", "/path", seenResources);

// Since the resource does not have a blob_id, the function should return NOT_FOUND
expect(result).toBe(HttpStatusCodes.NOT_FOUND);
});

test("should return NOT_FOUND if dynamic fields are not found", async () => {
const seenResources = new Set<string>();

// Mock to return no redirect
(checkRedirect as any).mockResolvedValueOnce(null);

// Mock to simulate that dynamic fields are not found
multiGetObjects.mockReturnValue([
{data: {bcs: {dataType: "moveObject"}}},
{},
]);

const result = await fetchResource(mockClient, "0x1", "/path", seenResources);

// Check that the function returns NOT_FOUND
expect(result).toBe(HttpStatusCodes.NOT_FOUND);
});
});

0 comments on commit fb73702

Please sign in to comment.