All files / tests/helpers metadata.ts

93.15% Statements 68/73
84% Branches 21/25
100% Functions 13/13
95.58% Lines 65/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 1822x 2x             2x                 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x                       2x 2x 2x 2x         2x 2x                     100x 100x 100x       100x             31x 31x 252x     2407x             31x 252x   31x       42x           1x           7x       11x       6x 6x 39x       6x 6x           31x 31x           31x 31x 56x       56x   6x 6x   7x 7x   10x 10x   7x 7x   11x 11x   9x 9x   6x 6x       56x                     31x    
import { Field } from "o1js";
import {
  Metadata,
  MetadataTree,
  Text,
  MetadataFieldType,
  MetadataFieldTypeValues,
} from "../../src/metadata";
import {
  uniqueNamesGenerator,
  names,
  adjectives,
  colors,
  animals,
  countries,
  languages,
} from "unique-names-generator";
import { pinJSON } from "./ipfs";
import { serializeIndexedMap, IndexedMapSerialized } from "zkcloudworker";
import { PINATA_JWT } from "../../env.json";
export {
  randomMetadata,
  randomName,
  randomText,
  randomImage,
  randomBanner,
  randomURL,
  randomField,
  randomTree,
  randomMap,
};
 
const NUMBER_OF_TRAITS = 5;
const NUMBER_OF_TREE_ENTRIES = 20;
 
async function randomMetadata(
  params: { includePrivateTraits?: boolean; includeBanner?: boolean } = {}
): Promise<{
  name: string;
  ipfsHash: string;
  metadataRoot: Field;
  privateMetadata: string;
  serializedMap: IndexedMapSerialized;
  map: Metadata;
}> {
  const { includePrivateTraits = true, includeBanner = false } = params;
  const metadata = randomMap({ includePrivateTraits, includeBanner });
  const privateMetadata = JSON.stringify(metadata.toJSON(true), null, 2);
  const ipfsHash = await pinJSON({
    data: metadata.toJSON(false),
    name: "metadata",
    auth: PINATA_JWT,
  });
  Iif (!ipfsHash) throw new Error("Failed to pin metadata");
  return {
    name: metadata.name,
    ipfsHash,
    metadataRoot: metadata.map.root,
    privateMetadata,
    serializedMap: serializeIndexedMap(metadata.map),
    map: metadata,
  };
}
 
function randomName() {
  let counter = 0;
  while (true) {
    const name = uniqueNamesGenerator({
      dictionaries: [names],
      length: 1,
    });
    if (name.length <= 30) return name;
    counter++;
    Iif (counter > 1000) throw new Error("Too many retries");
  }
}
 
function randomText() {
  const length = Math.floor(Math.random() * 20) + 1;
  const words = Array.from({ length }, () =>
    uniqueNamesGenerator({
      dictionaries: [
        ...[adjectives, names, colors, animals, countries, languages].sort(
          () => Math.random() - 0.5
        ),
      ],
      length: 6,
      separator: " ",
    })
  );
  const text = words
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
    .join(". ");
  return text;
}
 
function randomImage() {
  return `https://picsum.photos/seed/${Math.floor(
    Math.random() * 10000000
  )}/540/670`;
}
 
function randomBanner() {
  return `https://picsum.photos/seed/${Math.floor(
    Math.random() * 10000000
  )}/1920/300`;
}
 
function randomURL() {
  return `https://example.com/${randomName()}`;
}
 
function randomField() {
  return Field.random();
}
 
function randomTree() {
  const height = Math.floor(Math.random() * 240) + 10;
  const numberOfElements = Math.floor(Math.random() * NUMBER_OF_TREE_ENTRIES);
  const values = Array.from({ length: numberOfElements }, (_, i) => ({
    key: BigInt(i),
    value: Field.random(),
  }));
  const tree = new MetadataTree(height, values);
  return tree;
}
 
function randomMap(
  params: { includePrivateTraits?: boolean; includeBanner?: boolean } = {}
): Metadata {
  const { includePrivateTraits = true, includeBanner = false } = params;
  const map = new Metadata({
    name: randomName(),
    description: Math.random() < 0.7 ? randomText() : undefined,
    image: randomImage(),
    banner: includeBanner ? randomBanner() : undefined,
  });
  const numberOfTraits = Math.floor(Math.random() * NUMBER_OF_TRAITS);
  for (let i = 0; i < numberOfTraits; i++) {
    const type = Object.keys(MetadataFieldTypeValues)[
      Math.floor(Math.random() * Object.keys(MetadataFieldTypeValues).length)
    ] as MetadataFieldType;
    let value: string | Text | Field | Metadata | MetadataTree;
    switch (type) {
      case "string":
        value = randomName();
        break;
      case "text":
        value = randomText();
        break;
      case "image":
        value = randomImage();
        break;
      case "url":
        value = randomURL();
        break;
      case "field":
        value = randomField();
        break;
      case "map":
        value = randomMap();
        break;
      case "tree":
        value = randomTree();
        break;
      default:
        throw new Error(`Unknown type: ${type}`);
    }
    map.addTrait({
      key: randomName(),
      type,
      value,
      isPrivate: includePrivateTraits
        ? Math.random() < 0.3
          ? undefined
          : Math.random() < 0.5
        : false,
    });
  }
  return map;
}