/ tests / unit / utils.test.ts
utils.test.ts
  1  import { describe, expect, test } from "vitest";
  2  import * as utils from "@app/lib/utils";
  3  
  4  describe("Format functions", () => {
  5    test.each([
  6      {
  7        id: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
  8        expected: "rad:zKtT7D…19WzjT",
  9      },
 10    ])("formatRepositoryId $id => $expected", ({ id, expected }) => {
 11      expect(utils.formatRepositoryId(id)).toEqual(expected);
 12    });
 13  
 14    test.each([
 15      {
 16        input: "<TR> Hello `new` world",
 17        expected: "<TR> Hello <code>new</code> world",
 18      },
 19      { input: "Hello `new` world", expected: "Hello <code>new</code> world" },
 20      {
 21        input: "Hello `new` world `radicle`",
 22        expected: "Hello <code>new</code> world <code>radicle</code>",
 23      },
 24      { input: "Hello `` world", expected: "Hello `` world" },
 25      { input: "Hello `", expected: "Hello `" },
 26      { input: "Hello", expected: "Hello" },
 27    ])("formatInlineTitle $input => $expected", ({ input, expected }) => {
 28      expect(utils.formatInlineTitle(input)).toEqual(expected);
 29    });
 30  
 31    test.each([
 32      {
 33        id: "did:key:z6MkmzRwg47UWQxczLLLFfkEwpBGitjzJ1vKPE8U9ymd6fz6",
 34        expected: "did:key:z6Mkmz…md6fz6",
 35      },
 36      {
 37        id: "z6MkmzRwg47UWQxczLLLFfkEwpBGitjzJ1vKPE8U9ymd6fz6",
 38        expected: "did:key:z6Mkmz…md6fz6",
 39      },
 40    ])("formatNodeId $id => $expected", ({ id, expected }) => {
 41      expect(utils.formatNodeId(id)).toEqual(expected);
 42    });
 43  
 44    test.each([
 45      {
 46        id: "rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5",
 47        expected: "rad:z4V1sj…p4DtH5",
 48      },
 49      {
 50        id: "z4V1sjrXqjvFdnCUbxPFqd5p4DtH5",
 51        expected: "rad:z4V1sj…p4DtH5",
 52      },
 53    ])("formatRepositoryId $id => $expected", ({ id, expected }) => {
 54      expect(utils.formatRepositoryId(id)).toEqual(expected);
 55    });
 56  
 57    test.each([
 58      { commit: "a8a6a979a6261a2ec1ea85fc9a65a4a30aa22cc8", expected: "a8a6a97" },
 59      { commit: "a8a6a97", expected: "a8a6a97" },
 60    ])("formatCommit $commit => $expected", ({ commit, expected }) => {
 61      expect(utils.formatCommit(commit)).toEqual(expected);
 62    });
 63  });
 64  
 65  describe("String Assertions", () => {
 66    test.each([
 67      { path: "README.md", expected: true },
 68      { path: "README.mkd", expected: true },
 69      { path: "README.markdown", expected: true },
 70      { path: "", expected: false },
 71    ])("isMarkdownPath $path => $expected", ({ path, expected }) => {
 72      expect(utils.isMarkdownPath(path)).toEqual(expected);
 73    });
 74  
 75    test.each([
 76      { url: "https://app.radicle.xyz", expected: true },
 77      { url: "http://app.radicle.xyz", expected: true },
 78      { url: "http://app", expected: true },
 79      { url: "://app", expected: false },
 80      { url: "//app", expected: false },
 81      { url: "app", expected: false },
 82    ])("isUrl $url => $expected", ({ url, expected }) => {
 83      expect(utils.isUrl(url)).toBe(expected);
 84    });
 85  });
 86  
 87  describe("Parse Functions", () => {
 88    test.each([
 89      {
 90        input: "rad:z6MkmzRwg47UWQxczLLLFfkEwpBGitjzJ1vKPE8U9ymd6fz6",
 91        expected: undefined,
 92      },
 93      {
 94        input: "did:key:z6Mkmz…md6fz6",
 95        expected: undefined,
 96      },
 97      {
 98        input: "zlatan",
 99        expected: undefined,
100      },
101      {
102        input: "z6MkmzRwg47UWQxczLLLFfkEwpBGitjzJ1vKPE8U9ymd6fz6",
103        expected: {
104          prefix: "did:key:",
105          pubkey: "z6MkmzRwg47UWQxczLLLFfkEwpBGitjzJ1vKPE8U9ymd6fz6",
106        },
107      },
108      {
109        input: "did:key:z6MkmzRwg47UWQxczLLLFfkEwpBGitjzJ1vKPE8U9ymd6fz6",
110        expected: {
111          prefix: "did:key:",
112          pubkey: "z6MkmzRwg47UWQxczLLLFfkEwpBGitjzJ1vKPE8U9ymd6fz6",
113        },
114      },
115    ])("parseNodeId", ({ input, expected }) => {
116      expect(utils.parseNodeId(input)).toEqual(expected);
117    });
118  });
119  
120  describe("Path Manipulation", () => {
121    test.each([
122      {
123        imagePath: "/assets/images/tux.png",
124        base: "/",
125        origin: "https://app.radicle.xyz",
126        expected: "assets/images/tux.png",
127      },
128      {
129        imagePath: "/tux.md",
130        base: "/components/assets/README.md",
131        origin: "http://localhost:3000",
132        expected: "tux.md",
133      },
134      {
135        imagePath: "assets/images/tux.png",
136        base: "/",
137        origin: "https://app.radicle.xyz",
138        expected: "assets/images/tux.png",
139      },
140      {
141        imagePath: "assets/images/tux.png",
142        base: "/",
143        origin: "http://localhost:3000",
144        expected: "assets/images/tux.png",
145      },
146      {
147        imagePath: "../tux.png",
148        base: "/components/assets/README.md",
149        origin: "http://localhost:3000",
150        expected: "components/tux.png",
151      },
152      {
153        imagePath: "../tux.png",
154        base: "/components/assets/",
155        origin: "http://localhost:3000",
156        expected: "components/tux.png",
157      },
158      {
159        imagePath: "../../tux.png",
160        base: "/components/assets/images/README.md",
161        origin: "http://localhost:3000",
162        expected: "components/tux.png",
163      },
164    ])(
165      "canonicalize origin: $origin base: $base, path: $imagePath => $expected",
166      ({ imagePath, base, expected, origin }) => {
167        expect(utils.canonicalize(imagePath, base, origin)).toEqual(expected);
168      },
169    );
170  });
171  
172  describe("Date Manipulation", () => {
173    test.each([
174      { from: new Date("2022-01-01"), to: new Date("2022-02-01"), expected: 31 },
175      { from: new Date("2022-01-01"), to: new Date("2022-01-02"), expected: 1 },
176      { from: new Date("2022-01-01"), to: new Date("2022-01-01"), expected: 0 },
177    ])("getDaysPassed expected: $expected ", ({ from, to, expected }) => {
178      expect(utils.getDaysPassed(from, to)).toEqual(expected);
179    });
180    test.each([
181      {
182        from: new Date("2022-01-01 12:00:00"),
183        to: new Date("2022-01-01 12:00:00"),
184        expected: "now",
185      },
186      {
187        from: new Date("2022-01-01 12:00:00"),
188        to: new Date("2022-01-01 12:00:01"),
189        expected: "1 second ago",
190      },
191      {
192        from: new Date("2022-01-01 12:00:00"),
193        to: new Date("2022-01-01 12:01:01"),
194        expected: "1 minute ago",
195      },
196      {
197        from: new Date("2022-01-01 12:00:00"),
198        to: new Date("2022-01-01 13:01:01"),
199        expected: "1 hour ago",
200      },
201      {
202        from: new Date("2022-01-01 12:00:00"),
203        to: new Date("2022-01-02 13:01:01"),
204        expected: "yesterday",
205      },
206      {
207        from: new Date("2022-01-01 12:00:00"),
208        to: new Date("2022-01-04 13:01:01"),
209        expected: "3 days ago",
210      },
211      {
212        from: new Date("2022-01-01 12:00:00"),
213        to: new Date("2022-02-02 13:01:01"),
214        expected: "last month",
215      },
216      {
217        from: new Date("2022-01-01 12:00:00"),
218        to: new Date("2022-04-02 13:01:01"),
219        expected: "3 months ago",
220      },
221      {
222        from: new Date("2022-01-01 12:00:00"),
223        to: new Date("2023-04-02 12:00:00"),
224        expected: "more than a year ago",
225      },
226      {
227        from: new Date("2022-03-05 12:00:00"),
228        to: new Date("2026-04-02 12:00:00"),
229        expected: "more than a year ago",
230      },
231    ])("formatTimestamp expected: $expected", ({ from, to, expected }) => {
232      expect(utils.formatTimestamp(from.getTime() / 1000, to.getTime())).toEqual(
233        expected,
234      );
235    });
236  });