/ packages / schema / mod_test.ts
mod_test.ts
  1  import { assertEquals } from "@std/assert";
  2  import { extractEntriesFromHAMT, fetchAndDecode } from "@massmarket/utils";
  3  import type { CodecValue } from "@massmarket/utils/codec";
  4  import { Listing, Manifest, Order } from "./mod.ts";
  5  
  6  type Rmap = Map<string, Rmap>;
  7  type TestVector = Map<
  8    string,
  9    Array<Map<string, Map<string, Map<string, Map<string, CodecValue>>>>>
 10  >;
 11  Deno.test("unpack manifest vectors", async (t) => {
 12    const manifestOkayVector = await fetchAndDecode("ManifestOkay");
 13    const manifestOkayVectorSnapshots = manifestOkayVector.get(
 14      "Snapshots",
 15    ) as Array<Rmap>;
 16  
 17    const vector = manifestOkayVectorSnapshots.map(
 18      (snapshot) => {
 19        return {
 20          name: snapshot.get("Name") as unknown as string,
 21          value: snapshot?.get("After")?.get("Value")?.get("Manifest")!,
 22        };
 23      },
 24    ) || [];
 25  
 26    for (const manifest of vector) {
 27      await t.step(manifest.name as string, () => {
 28        const unpacked = Manifest.fromCBOR(manifest.value);
 29        assertEquals(unpacked.asCBORMap(), manifest.value);
 30      });
 31    }
 32  });
 33  
 34  Deno.test("unpack listing vectors", async (t) => {
 35    const listingOkayVector = await fetchAndDecode("ListingOkay") as TestVector;
 36  
 37    const vectors = (listingOkayVector?.get("Snapshots") as unknown as [])?.map(
 38      (snapshot: Rmap) => {
 39        const values = extractEntriesFromHAMT(
 40          snapshot!.get("After")!.get("Value")!.get("Listings"),
 41        );
 42        return {
 43          name: snapshot!.get("Name"),
 44          values: values,
 45        };
 46      },
 47    ) || [];
 48  
 49    for (const vector of vectors) {
 50      if (!vector || !vector.values || !(vector.values instanceof Map)) {
 51        continue;
 52      }
 53      for (const [id, listing] of vector.values.entries()) {
 54        await t.step(`${vector.name} - ${id}`, () => {
 55          const unpacked = Listing.fromCBOR(listing);
 56          assertEquals(unpacked.asCBORMap(), listing);
 57        });
 58      }
 59    }
 60  });
 61  
 62  Deno.test("unpack order vectors", async () => {
 63    const orderOkayVector = await fetchAndDecode("OrderOkay");
 64  
 65    const orders = (orderOkayVector?.get("Snapshots") as unknown as [])?.map(
 66      (snapshot: Rmap) => {
 67        const hamtNode = snapshot?.get("After")?.get("Value")?.get("Orders");
 68        return extractEntriesFromHAMT(hamtNode);
 69      },
 70    ) || [];
 71  
 72    for (const orderMap of orders) {
 73      if (!orderMap || !(orderMap instanceof Map)) {
 74        continue;
 75      }
 76      for (const [_id, order] of orderMap.entries()) {
 77        const unpacked = Order.fromCBOR(order);
 78        assertEquals(unpacked.asCBORMap(), order);
 79      }
 80    }
 81  });
 82  
 83  // Deno.test("AcceptedCurrencyMap addAddress/removeAddress", () => {
 84  //   const currencies = new AcceptedCurrencyMap();
 85  //   const chainId = 1;
 86  //   const address1 = new Uint8Array([1, 2, 3]);
 87  //   const address2 = new Uint8Array([4, 5, 6]);
 88  
 89  //   currencies.addAddress(chainId, address1, false);
 90  //   currencies.addAddress(chainId, address2, true);
 91  //   const result1 = currencies.getAddressesByChainID(chainId);
 92  //   assertEquals(result1?.size, 2);
 93  //   assertEquals(result1?.get(address1)?.get("IsContract"), false);
 94  //   assertEquals(result1?.get(address2)?.get("IsContract"), true);
 95  
 96  //   currencies.removeAddress(chainId, address1);
 97  //   const result2 = currencies.getAddressesByChainID(chainId);
 98  //   assertEquals(result2?.size, 1);
 99  //   assertEquals(result2?.get(address2)?.get("IsContract"), true);
100  // });
101  
102  // Deno.test("PayeeMap addAddress", () => {
103  //   const payees = new PayeeMap();
104  
105  //   const address1 = new Uint8Array([1, 2, 3]);
106  //   const address2 = new Uint8Array([4, 5, 6]);
107  
108  //   payees.addAddress(1, address1, false);
109  //   payees.addAddress(2, address2, true);
110  //   assertEquals(payees.get(1)?.get(address1)?.CallAsContract, false);
111  //   assertEquals(payees.get(2)?.get(address2)?.CallAsContract, true);
112  // });