aboutsummaryrefslogtreecommitdiff
path: root/src/metadata.test.ts
blob: 439bf7e22624158f409275e0c1649285fdeac61e (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import Metadata, { isError, EnvConfig, HeadIdentity } from "./metadata";
import { Db, MongoClient, Collection } from "mongodb";
import { BranchNotFoundError } from "./errors";
jest.mock("mongodb");

const defaultEnvConfig = {
  token: "llama",
  uploadLimit: 12,
  hostDir: "pineapple",
  publicDir: ".dir",
  stage1: 132,
  stage2: 1.0,
};
const defaultMetadata = () =>
  new Metadata(new Db({} as MongoClient, ""), defaultEnvConfig as EnvConfig);

describe("isError", () => {
  it("should return false when object is a HeadContext", () => {
    // Arrange
    const obj = {
      commit: "fae10429d",
      format: "15-minute quarters",
    };

    // Act
    const result = isError(obj);

    // Assert
    expect(result).toEqual(false);
  });

  it("should return true when object is a BranchNotFoundError", () => {
    // Arrange
    const error = new BranchNotFoundError();

    // Act
    const result = isError(error);

    // Assert
    expect(result).toEqual(true);
  });
});

describe("getHeadCommit", () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it("should return commit information if the repository exists and branch is a string", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const organization = "kjhoerr";
    const repository = "ao-coverage";
    const branch = "aaaaa";
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const repositoryObject = {
      value: "repository",
      branches: { [branch]: { head: "yay" } },
    };
    const findMethod = jest
      .spyOn(Collection.prototype, "findOne")
      .mockImplementation(() => Promise.resolve(repositoryObject));

    // Act
    const result = await metadata.getHeadCommit(
      organization,
      repository,
      branch
    );

    // Assert
    expect(isError(result)).toEqual(false);
    expect(collectionMethod).toHaveBeenCalledTimes(1);
    expect(findMethod).toHaveBeenCalledTimes(1);
    if (!isError(result)) {
      expect(result.commit).toEqual(repositoryObject.branches[branch].head);
      expect(result.format).toEqual("tarpaulin");
    }
  });

  it("should return commit information if the repository exists and branch is a HeadContext", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const organization = "kjhoerr";
    const repository = "ao-coverage";
    const branch = "aaaaa";
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const headContext = { commit: "yay", format: "big-stick" };
    const repositoryObject = {
      value: "repository",
      branches: { [branch]: { head: headContext } },
    };
    const findMethod = jest
      .spyOn(Collection.prototype, "findOne")
      .mockImplementation(() => Promise.resolve(repositoryObject));

    // Act
    const result = await metadata.getHeadCommit(
      organization,
      repository,
      branch
    );

    // Assert
    expect(isError(result)).toEqual(false);
    expect(collectionMethod).toHaveBeenCalledTimes(1);
    expect(findMethod).toHaveBeenCalledTimes(1);
    if (!isError(result)) {
      expect(result.commit).toEqual(headContext.commit);
      expect(result.format).toEqual(headContext.format);
    }
  });

  it("should return BranchNotFoundError if the repository exists but the branch does not exist", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const organization = "kjhoerr";
    const repository = "ao-coverage";
    const branch = "aaaaa";
    const fakeBranch = "main";
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const repositoryObject = {
      value: "repository",
      branches: { [fakeBranch]: { head: "yep" } },
    };
    const findMethod = jest
      .spyOn(Collection.prototype, "findOne")
      .mockImplementation(() => Promise.resolve(repositoryObject));

    // Act
    const result = await metadata.getHeadCommit(
      organization,
      repository,
      branch
    );

    // Assert
    expect(fakeBranch).not.toEqual(branch);
    expect(isError(result)).toEqual(true);
    expect(collectionMethod).toHaveBeenCalledTimes(1);
    expect(findMethod).toHaveBeenCalledTimes(1);
  });

  it("should return BranchNotFoundError if the repository does not exist", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const organization = "kjhoerr";
    const repository = "ao-coverage";
    const branch = "aaaaa";
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const findMethod = jest
      .spyOn(Collection.prototype, "findOne")
      .mockImplementation(() => Promise.resolve(null));

    // Act
    const result = await metadata.getHeadCommit(
      organization,
      repository,
      branch
    );

    // Assert
    expect(isError(result)).toEqual(true);
    expect(collectionMethod).toHaveBeenCalledTimes(1);
    expect(findMethod).toHaveBeenCalledTimes(1);
  });

  it("should return with rejected promise on Mongo error", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const organization = "kjhoerr";
    const repository = "ao-coverage";
    const branch = "aaaaa";
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const findMethod = jest
      .spyOn(Collection.prototype, "findOne")
      .mockImplementation(() => Promise.reject("uh-oh"));

    // Act
    const result = metadata.getHeadCommit(organization, repository, branch);

    // Assert
    expect(result).rejects.toEqual("uh-oh");
    expect(collectionMethod).toHaveBeenCalledTimes(1);
    expect(findMethod).toHaveBeenCalledTimes(1);
  });
});

describe("updateBranch", () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it("should update the repository with new branch information", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const updateMethod = jest
      .spyOn(Collection.prototype, "findOneAndUpdate")
      .mockImplementation(() =>
        Promise.resolve({ ok: 1, value: "repository" })
      );
    const insertMethod = jest
      .spyOn(Collection.prototype, "insertOne")
      .mockImplementation(() => Promise.resolve({ acknowledged: true }));
    const identity: HeadIdentity = {
      organization: "kjhoerr",
      repository: "ao-coverage",
      branch: "trunk",
      head: {
        commit: "yep",
        format: "xml",
      },
    };

    // Act
    const result = await metadata.updateBranch(identity);

    // Assert
    expect(result).toEqual(true);
    expect(collectionMethod).toHaveBeenCalledTimes(1);
    expect(updateMethod).toHaveBeenCalledTimes(1);
    expect(insertMethod).not.toHaveBeenCalled();
    const document = updateMethod.mock.calls[0][0];
    expect(document.organization).toEqual(identity.organization);
    expect(document.name).toEqual(identity.repository);
  });

  it("should call create repository if it does not exist yet", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const updateMethod = jest
      .spyOn(Collection.prototype, "findOneAndUpdate")
      .mockImplementation(() => Promise.resolve({}));
    const insertMethod = jest
      .spyOn(Collection.prototype, "insertOne")
      .mockImplementation(() => Promise.resolve({ acknowledged: true }));
    const identity: HeadIdentity = {
      organization: "kjhoerr",
      repository: "ao-coverage",
      branch: "trunk",
      head: {
        commit: "yep",
        format: "xml",
      },
    };

    // Act
    const result = await metadata.updateBranch(identity);

    // Assert
    expect(result).toEqual(true);
    expect(collectionMethod).toHaveBeenCalledTimes(2);
    expect(updateMethod).toHaveBeenCalledTimes(1);
    // Metadata.createRepository was called
    expect(insertMethod).toHaveBeenCalledTimes(1);
  });

  it("should return with rejected promise on Mongo error", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const updateMethod = jest
      .spyOn(Collection.prototype, "findOneAndUpdate")
      .mockImplementation(() => Promise.reject("Success!!!!!!!!"));
    const insertMethod = jest
      .spyOn(Collection.prototype, "insertOne")
      .mockImplementation(() => Promise.resolve({ acknowledged: true }));
    const identity: HeadIdentity = {
      organization: "kjhoerr",
      repository: "ao-coverage",
      branch: "trunk",
      head: {
        commit: "yep",
        format: "xml",
      },
    };

    // Act
    const result = metadata.updateBranch(identity);

    // Assert
    expect(result).rejects.toEqual("Success!!!!!!!!");
    expect(collectionMethod).toHaveBeenCalledTimes(1);
    expect(updateMethod).toHaveBeenCalledTimes(1);
    expect(insertMethod).not.toHaveBeenCalled();
  });
});

describe("createRepository", () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it("should return true upon adding a new repository document", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const insertMethod = jest
      .spyOn(Collection.prototype, "insertOne")
      .mockImplementation(() => Promise.resolve({ acknowledged: true }));
    const identity: HeadIdentity = {
      organization: "kjhoerr",
      repository: "ao-coverage",
      branch: "trunk",
      head: {
        commit: "yep",
        format: "xml",
      },
    };

    // Act
    const result = await metadata.createRepository(identity);

    // Assert
    expect(result).toEqual(true);
    expect(collectionMethod).toHaveBeenCalledTimes(1);
    expect(insertMethod).toHaveBeenCalledTimes(1);
    const document = insertMethod.mock.calls[0][0];
    expect(document.organization).toEqual(identity.organization);
    expect(document.name).toEqual(identity.repository);
    expect(document.branches[identity.branch]).toBeDefined();
    const branch = document.branches[identity.branch];
    expect(branch.head).toEqual(identity.head);
  });

  it("should return with rejected promise on Mongo error", async () => {
    // Arrange
    const metadata = defaultMetadata();
    const collectionMethod = jest
      .spyOn(Db.prototype, "collection")
      .mockImplementation(() => new Collection());
    const insertMethod = jest
      .spyOn(Collection.prototype, "insertOne")
      .mockImplementation(() => Promise.reject("fooey"));
    const identity: HeadIdentity = {
      organization: "kjhoerr",
      repository: "ao-coverage",
      branch: "trunk",
      head: {
        commit: "yep",
        format: "xml",
      },
    };

    // Act
    const result = metadata.createRepository(identity);

    // Assert
    expect(result).rejects.toEqual("fooey");
    expect(collectionMethod).toHaveBeenCalledTimes(1);
    expect(insertMethod).toHaveBeenCalledTimes(1);
  });
});

describe("getToken", () => {
  it("should return the token from EnvConfig", () => {
    // Arrange
    const metadata = defaultMetadata();

    // Act
    const result = metadata.getToken();

    // Assert
    expect(result).toEqual(defaultEnvConfig.token);
  });
});

describe("getUploadLimit", () => {
  it("should return the uploadLimit from EnvConfig", () => {
    // Arrange
    const metadata = defaultMetadata();

    // Act
    const result = metadata.getUploadLimit();

    // Assert
    expect(result).toEqual(defaultEnvConfig.uploadLimit);
  });
});

describe("getHostDir", () => {
  it("should return the hostDir from EnvConfig", () => {
    // Arrange
    const metadata = defaultMetadata();

    // Act
    const result = metadata.getHostDir();

    // Assert
    expect(result).toEqual(defaultEnvConfig.hostDir);
  });
});

describe("getPublicDir", () => {
  it("should return the publicDir from EnvConfig", () => {
    // Arrange
    const metadata = defaultMetadata();

    // Act
    const result = metadata.getPublicDir();

    // Assert
    expect(result).toEqual(defaultEnvConfig.publicDir);
  });
});

describe("getGradientStyle", () => {
  it("should return the stages for GradientStyle from EnvConfig", () => {
    // Arrange
    const metadata = defaultMetadata();

    // Act
    const result = metadata.getGradientStyle();

    // Assert
    expect(result.stage1).toEqual(defaultEnvConfig.stage1);
    expect(result.stage2).toEqual(defaultEnvConfig.stage2);
  });
});