/ packages / utils / mod_test.ts
mod_test.ts
  1  import { expect } from "@std/expect";
  2  import { extractEntriesFromHAMT, fetchAndDecode } from "./mod.ts";
  3  import type { CodecValue } from "./codec.ts";
  4  
  5  let snapshots: Map<string, Map<string, CodecValue>>[];
  6  Deno.test("fetchAndDecode with ShopOkay vector", async () => {
  7    const testVector = await fetchAndDecode("ShopOkay");
  8  
  9    // Verify the returned object is a Map (TestVector)
 10    expect(testVector instanceof Map).toBe(true);
 11  
 12    // Verify it contains expected properties
 13    expect(testVector.has("Snapshots")).toBe(true);
 14  
 15    // Get the snapshots array
 16    snapshots = testVector.get("Snapshots") as Map<
 17      string,
 18      Map<string, CodecValue>
 19    >[];
 20    expect(Array.isArray(snapshots)).toBe(true);
 21  
 22    expect(snapshots.length).toBeGreaterThan(0);
 23  });
 24  
 25  Deno.test("hamt extraction gives correct keys (accounts)", () => {
 26    const firstSnapshot = snapshots[5]; // test case "add-guest-account"
 27    expect(firstSnapshot instanceof Map).toBe(true);
 28  
 29    // Check if we can access "After" -> "Value" -> "Accounts"
 30    const after = firstSnapshot.get("After");
 31    expect(after instanceof Map).toBe(true);
 32  
 33    if (after) {
 34      const value = after.get("Value");
 35      expect(value instanceof Map).toBe(true);
 36      if (value instanceof Map && value.has("Accounts")) {
 37        const accounts = value.get("Accounts");
 38        expect(accounts).toBeInstanceOf(Array);
 39  
 40        const accountMap = extractEntriesFromHAMT(accounts);
 41        if (accountMap) {
 42          expect(accountMap instanceof Map).toBe(true);
 43          if (accountMap instanceof Map) {
 44            // Verify all keys in the account map are Uint8Array(20)
 45            for (const key of accountMap.keys()) {
 46              expect(key instanceof Uint8Array).toBe(true);
 47              const keyArr = key as Uint8Array;
 48              expect(keyArr.length).toBe(20);
 49            }
 50          }
 51        }
 52      }
 53    }
 54  });
 55  
 56  Deno.test("hamt extraction gives correct keys (listings)", () => {
 57    // Find a snapshot with listings
 58    const listingSnapshot = snapshots.filter((snapshot) => {
 59      const after = snapshot.get("After");
 60      if (after instanceof Map) {
 61        const value = after.get("Value");
 62        if (value instanceof Map) {
 63          const listings = value.get("Listings");
 64          return listings !== undefined;
 65        }
 66      }
 67      return false;
 68    });
 69  
 70    expect(listingSnapshot.length).toBeGreaterThan(0);
 71  
 72    for (const snapshot of listingSnapshot) {
 73      const after = snapshot.get("After");
 74      expect(after instanceof Map).toBe(true);
 75  
 76      if (after) {
 77        const value = after.get("Value");
 78        expect(value instanceof Map).toBe(true);
 79  
 80        if (value instanceof Map && value.has("Listings")) {
 81          const listings = value.get("Listings");
 82          expect(listings).toBeInstanceOf(Array);
 83  
 84          const listingMap = extractEntriesFromHAMT(listings);
 85          expect(listingMap instanceof Map).toBe(true);
 86  
 87          if (listingMap instanceof Map) {
 88            // Verify all keys in the listing map are numbers
 89            for (const key of listingMap.keys()) {
 90              expect(typeof key).toBe("number");
 91            }
 92          }
 93        }
 94      }
 95    }
 96  });
 97  
 98  Deno.test("hamt extraction gives correct keys (orders)", () => {
 99    // Find a snapshot with orders
100    const orderSnapshot = snapshots.filter((snapshot) => {
101      const after = snapshot.get("After");
102      if (after instanceof Map) {
103        const value = after.get("Value");
104        if (value instanceof Map) {
105          const orders = value.get("Orders");
106          return orders !== undefined;
107        }
108      }
109      return false;
110    });
111  
112    // If no snapshot has orders, this test is inconclusive but shouldn't fail
113    expect(orderSnapshot.length).toBeGreaterThan(0);
114  
115    for (const snapshot of orderSnapshot) {
116      const after = snapshot.get("After");
117      expect(after instanceof Map).toBe(true);
118  
119      if (after) {
120        const value = after.get("Value");
121        expect(value instanceof Map).toBe(true);
122  
123        if (value instanceof Map && value.has("Orders")) {
124          const orders = value.get("Orders");
125          expect(orders).toBeInstanceOf(Array);
126  
127          const orderMap = extractEntriesFromHAMT(orders);
128          expect(orderMap instanceof Map).toBe(true);
129  
130          if (orderMap instanceof Map) {
131            // Verify all keys in the order map are numbers
132            for (const key of orderMap.keys()) {
133              expect(typeof key).toBe("number");
134            }
135          }
136        }
137      }
138    }
139  });