|
| 1 | +import { getRepoProviders, detect } from "../lib/autodetect"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | + |
| 4 | +const mybinderConfig = JSON.parse( |
| 5 | + readFileSync(`${__dirname}/fixtures/mybinder.config.json`, { |
| 6 | + encoding: "utf-8", |
| 7 | + }), |
| 8 | +); |
| 9 | + |
| 10 | +// Mock fetch() |
| 11 | +// https://www.leighhalliday.com/mock-fetch-jest |
| 12 | +global.fetch = jest.fn((url) => { |
| 13 | + if (url == "https://binder.example.org/_config") { |
| 14 | + return Promise.resolve({ |
| 15 | + json: () => Promise.resolve(mybinderConfig), |
| 16 | + }); |
| 17 | + } |
| 18 | + return Promise.reject(`Unexpected URL ${url}`); |
| 19 | +}); |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + fetch.mockClear(); |
| 23 | +}); |
| 24 | + |
| 25 | +test("getRepoProviders requests the repo provider configs", async () => { |
| 26 | + const config = await getRepoProviders("https://binder.example.org"); |
| 27 | + expect(config).toEqual(mybinderConfig); |
| 28 | +}); |
| 29 | + |
| 30 | +test("detect returns null if no provider matches", async () => { |
| 31 | + const result = await detect( |
| 32 | + "https://binder.example.org", |
| 33 | + "https://github.com/binder-examples/conda/pulls", |
| 34 | + ); |
| 35 | + expect(result).toBeNull(); |
| 36 | +}); |
| 37 | + |
| 38 | +test("detect parses a repo with no path", async () => { |
| 39 | + const expected = { |
| 40 | + providerPrefix: "gh", |
| 41 | + repository: "binder-examples/conda", |
| 42 | + ref: null, |
| 43 | + path: null, |
| 44 | + pathType: null, |
| 45 | + providerName: "Fake GitHub", |
| 46 | + }; |
| 47 | + const result = await detect( |
| 48 | + "https://binder.example.org", |
| 49 | + "https://github.com/binder-examples/conda", |
| 50 | + ); |
| 51 | + expect(result).toEqual(expected); |
| 52 | +}); |
| 53 | + |
| 54 | +test("detect parses a repo with a ref but no path", async () => { |
| 55 | + const expected = { |
| 56 | + providerPrefix: "gh", |
| 57 | + repository: "binder-examples/conda", |
| 58 | + ref: "abc", |
| 59 | + path: null, |
| 60 | + pathType: null, |
| 61 | + providerName: "Fake GitHub", |
| 62 | + }; |
| 63 | + const result = await detect( |
| 64 | + "https://binder.example.org", |
| 65 | + "https://github.com/binder-examples/conda/tree/abc", |
| 66 | + ); |
| 67 | + expect(result).toEqual(expected); |
| 68 | +}); |
| 69 | + |
| 70 | +test("detect parses a repo with a ref and file path", async () => { |
| 71 | + const expected = { |
| 72 | + providerPrefix: "gh", |
| 73 | + repository: "binder-examples/conda", |
| 74 | + ref: "f00a783", |
| 75 | + path: "index.ipynb", |
| 76 | + pathType: "filepath", |
| 77 | + providerName: "Fake GitHub", |
| 78 | + }; |
| 79 | + const result = await detect( |
| 80 | + "https://binder.example.org", |
| 81 | + "https://github.com/binder-examples/conda/blob/f00a783/index.ipynb", |
| 82 | + ); |
| 83 | + expect(result).toEqual(expected); |
| 84 | +}); |
| 85 | + |
| 86 | +test("detect parses a repo with a ref and directory path", async () => { |
| 87 | + const expected = { |
| 88 | + providerPrefix: "gh", |
| 89 | + repository: "binder-examples/conda", |
| 90 | + ref: "f00a783", |
| 91 | + path: ".github/workflows", |
| 92 | + pathType: "urlpath", |
| 93 | + providerName: "Fake GitHub", |
| 94 | + }; |
| 95 | + const result = await detect( |
| 96 | + "https://binder.example.org", |
| 97 | + "https://github.com/binder-examples/conda/tree/f00a783/.github/workflows", |
| 98 | + ); |
| 99 | + expect(result).toEqual(expected); |
| 100 | +}); |
| 101 | + |
| 102 | +test("detect checks other repo providers", async () => { |
| 103 | + const expected = { |
| 104 | + providerPrefix: "gl", |
| 105 | + repository: "gitlab-org/gitlab-foss", |
| 106 | + ref: "v16.4.4", |
| 107 | + path: "README.md", |
| 108 | + pathType: "filepath", |
| 109 | + providerName: "GitLab.com", |
| 110 | + }; |
| 111 | + const result = await detect( |
| 112 | + "https://binder.example.org", |
| 113 | + "https://gitlab.com/gitlab-org/gitlab-foss/-/blob/v16.4.4/README.md", |
| 114 | + ); |
| 115 | + expect(result).toEqual(expected); |
| 116 | +}); |
0 commit comments