@rosen-bridge/utils 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/lib/constants.d.ts +0 -1
- package/dist/lib/constants.d.ts.map +1 -1
- package/dist/lib/constants.js +1 -2
- package/dist/lib/downloadRosenAssets.d.ts.map +1 -1
- package/dist/lib/downloadRosenAssets.js +5 -4
- package/dist/lib/downloadTssBinary.d.ts.map +1 -1
- package/dist/lib/downloadTssBinary.js +2 -2
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +2 -1
- package/dist/lib/types/index.d.ts +1 -0
- package/dist/lib/types/index.d.ts.map +1 -1
- package/dist/lib/types/index.js +1 -1
- package/dist/lib/utils/github.d.ts +221 -7
- package/dist/lib/utils/github.d.ts.map +1 -1
- package/dist/lib/utils/github.js +42 -11
- package/dist/lib/utils/rosen.d.ts +5 -0
- package/dist/lib/utils/rosen.d.ts.map +1 -1
- package/dist/lib/utils/rosen.js +6 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/lib/constants.ts +0 -1
- package/lib/downloadRosenAssets.ts +5 -3
- package/lib/downloadTssBinary.ts +61 -0
- package/lib/index.ts +1 -0
- package/lib/types/index.ts +2 -0
- package/lib/utils/github.ts +60 -10
- package/lib/utils/rosen.ts +7 -0
- package/package.json +1 -1
- package/tests/data/octokit.data.ts +88 -10
- package/tests/downloadRosenAssets.spec.ts +9 -9
- package/tests/downloadTssBinary.spec.ts +153 -0
- package/tests/mocks/octokit.mock.ts +4 -4
- package/tests/utils/github.spec.ts +173 -21
- package/tests/utils/rosen.spec.ts +39 -1
package/lib/constants.ts
CHANGED
|
@@ -9,6 +9,8 @@ import { isValidAssetName, truncateAssetName } from './utils/rosen';
|
|
|
9
9
|
|
|
10
10
|
import { RosenAssetsDownloadError } from './error';
|
|
11
11
|
|
|
12
|
+
const repo = 'contract';
|
|
13
|
+
|
|
12
14
|
/**
|
|
13
15
|
* Download all required Rosen assets (tokenMap and all chain address files) to a specific path
|
|
14
16
|
* @param chainType chain type (e.g. mainnet, testnet, etc.)
|
|
@@ -25,9 +27,9 @@ const downloadRosenAssets = async (
|
|
|
25
27
|
}
|
|
26
28
|
) => {
|
|
27
29
|
const getRelease = () => {
|
|
28
|
-
if (config?.tag) return getReleaseByTag(config.tag);
|
|
29
|
-
if (config?.includePrereleases) return findLatestRelease(chainType);
|
|
30
|
-
return findLatestStableRelease(chainType);
|
|
30
|
+
if (config?.tag) return getReleaseByTag(repo, config.tag);
|
|
31
|
+
if (config?.includePrereleases) return findLatestRelease(repo, chainType);
|
|
32
|
+
return findLatestStableRelease(repo, chainType);
|
|
31
33
|
};
|
|
32
34
|
try {
|
|
33
35
|
const release = await getRelease();
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import download from 'download';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
findLatestReleaseByPrefixTag,
|
|
5
|
+
findLatestStableReleaseByPrefixTag,
|
|
6
|
+
getReleaseByTag,
|
|
7
|
+
} from './utils/github';
|
|
8
|
+
import { isValidOS } from './utils/rosen';
|
|
9
|
+
|
|
10
|
+
import { RosenAssetsDownloadError } from './error';
|
|
11
|
+
|
|
12
|
+
const repo = 'sign-protocols';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Download release asset by tag to a specific path
|
|
16
|
+
* @param destinationPath where writes files
|
|
17
|
+
* @param config configs for including prereleases, set osName, getting specific release by tag or by regex tag
|
|
18
|
+
*/
|
|
19
|
+
const downloadTssBinary = async (
|
|
20
|
+
destinationPath: string,
|
|
21
|
+
config: {
|
|
22
|
+
osName: string;
|
|
23
|
+
tag: string;
|
|
24
|
+
regex: boolean;
|
|
25
|
+
includePrereleases?: boolean;
|
|
26
|
+
}
|
|
27
|
+
) => {
|
|
28
|
+
const getRelease = () => {
|
|
29
|
+
if (config.regex) {
|
|
30
|
+
if (config?.includePrereleases)
|
|
31
|
+
return findLatestReleaseByPrefixTag(repo, config.tag);
|
|
32
|
+
else return findLatestStableReleaseByPrefixTag(repo, config.tag);
|
|
33
|
+
} else return getReleaseByTag(repo, config.tag);
|
|
34
|
+
};
|
|
35
|
+
try {
|
|
36
|
+
const release = await getRelease();
|
|
37
|
+
|
|
38
|
+
if (release) {
|
|
39
|
+
await Promise.all([
|
|
40
|
+
...(release &&
|
|
41
|
+
release.assets
|
|
42
|
+
.filter((asset) => isValidOS(config.osName)(asset.name))
|
|
43
|
+
.map(async (asset) =>
|
|
44
|
+
download(asset.browser_download_url, destinationPath)
|
|
45
|
+
)),
|
|
46
|
+
]);
|
|
47
|
+
} else {
|
|
48
|
+
console.error(`No release found for [${config.osName}] OS name.`);
|
|
49
|
+
}
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error(
|
|
52
|
+
`An error occurred while trying to download release assets: ${error}`
|
|
53
|
+
);
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
console.error(error.stack);
|
|
56
|
+
}
|
|
57
|
+
throw new RosenAssetsDownloadError('', { cause: error });
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export { downloadTssBinary };
|
package/lib/index.ts
CHANGED
package/lib/types/index.ts
CHANGED
package/lib/utils/github.ts
CHANGED
|
@@ -3,19 +3,20 @@ import { Octokit } from 'octokit';
|
|
|
3
3
|
import { isValidAssetName } from './rosen';
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
|
-
CONTRACT_REPO_NAME,
|
|
7
6
|
DEFAULT_RELEASES_FETCHING_PAGE_SIZE,
|
|
8
7
|
ROSEN_BRIDGE_ORGANIZATION,
|
|
9
8
|
} from '../constants';
|
|
10
9
|
|
|
11
|
-
import { GithubRelease } from '../types';
|
|
10
|
+
import { GithubRelease, SupportedRepo } from '../types';
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
13
|
* Fetch a page of releases from Github Api in each iteration until there are no
|
|
15
14
|
* more releases.
|
|
15
|
+
* @param repoName
|
|
16
16
|
* @param pageSize
|
|
17
17
|
*/
|
|
18
18
|
async function* fetchReleasesPage(
|
|
19
|
+
repoName: SupportedRepo,
|
|
19
20
|
pageSize = DEFAULT_RELEASES_FETCHING_PAGE_SIZE
|
|
20
21
|
) {
|
|
21
22
|
const octokit = new Octokit();
|
|
@@ -25,7 +26,7 @@ async function* fetchReleasesPage(
|
|
|
25
26
|
while (true) {
|
|
26
27
|
const releasesPage = await octokit.rest.repos.listReleases({
|
|
27
28
|
owner: ROSEN_BRIDGE_ORGANIZATION,
|
|
28
|
-
repo:
|
|
29
|
+
repo: repoName,
|
|
29
30
|
per_page: pageSize,
|
|
30
31
|
page: currentPage,
|
|
31
32
|
});
|
|
@@ -42,12 +43,14 @@ async function* fetchReleasesPage(
|
|
|
42
43
|
/**
|
|
43
44
|
* Find the last release matching the predicate. If all releases are iterated and
|
|
44
45
|
* no matching release is found, return null.
|
|
46
|
+
* @param repoName
|
|
45
47
|
* @param predicate
|
|
46
48
|
*/
|
|
47
49
|
const findLastRelease = async (
|
|
50
|
+
repoName: SupportedRepo,
|
|
48
51
|
predicate: (release: GithubRelease) => boolean = () => true
|
|
49
52
|
) => {
|
|
50
|
-
const releasesPageIterator = fetchReleasesPage();
|
|
53
|
+
const releasesPageIterator = fetchReleasesPage(repoName);
|
|
51
54
|
|
|
52
55
|
for await (const releasesPage of releasesPageIterator) {
|
|
53
56
|
const foundRelease = releasesPage.find(predicate);
|
|
@@ -61,13 +64,14 @@ const findLastRelease = async (
|
|
|
61
64
|
|
|
62
65
|
/**
|
|
63
66
|
* get a GitHub release by its tag
|
|
67
|
+
* @param repoName
|
|
64
68
|
* @param tag
|
|
65
69
|
*/
|
|
66
|
-
const getReleaseByTag = async (tag: string) => {
|
|
70
|
+
const getReleaseByTag = async (repoName: SupportedRepo, tag: string) => {
|
|
67
71
|
const octokit = new Octokit();
|
|
68
72
|
const release = await octokit.rest.repos.getReleaseByTag({
|
|
69
73
|
owner: ROSEN_BRIDGE_ORGANIZATION,
|
|
70
|
-
repo:
|
|
74
|
+
repo: repoName,
|
|
71
75
|
tag,
|
|
72
76
|
});
|
|
73
77
|
|
|
@@ -92,28 +96,74 @@ const isStableReleaseForChainType =
|
|
|
92
96
|
(chainType: string) => (release: GithubRelease) =>
|
|
93
97
|
!release.prerelease && hasAssetForChainType(chainType)(release);
|
|
94
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Return a function which checks if tagPrefix is matched with release tag_name
|
|
101
|
+
* @param tagPrefix
|
|
102
|
+
*/
|
|
103
|
+
const hasMatchedTagPrefix = (tagPrefix: string) => (release: GithubRelease) => {
|
|
104
|
+
const regex = new RegExp(`^${tagPrefix}`);
|
|
105
|
+
return regex.test(release.tag_name);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Return a function which checks if a release is a stable (that is, non-prerelease),
|
|
110
|
+
* and tagPrefix is matched with release tag_name
|
|
111
|
+
* @param tagPrefix
|
|
112
|
+
*/
|
|
113
|
+
const isStableReleaseForRegexTagType =
|
|
114
|
+
(tagPrefix: string) => (release: GithubRelease) =>
|
|
115
|
+
!release.prerelease && hasMatchedTagPrefix(tagPrefix)(release);
|
|
116
|
+
|
|
95
117
|
/**
|
|
96
118
|
* Find latest release (prerelease or non-prerelease) having some asset matching
|
|
97
119
|
* a specific chain type
|
|
120
|
+
* @param repoName
|
|
98
121
|
* @param chainType
|
|
99
122
|
*/
|
|
100
|
-
const findLatestRelease = async (chainType: string) =>
|
|
101
|
-
findLastRelease(hasAssetForChainType(chainType));
|
|
123
|
+
const findLatestRelease = async (repoName: SupportedRepo, chainType: string) =>
|
|
124
|
+
findLastRelease(repoName, hasAssetForChainType(chainType));
|
|
102
125
|
|
|
103
126
|
/**
|
|
104
127
|
* Find latest stable (that is, non-prerelease) release having some asset matching
|
|
105
128
|
* a specific chain type
|
|
129
|
+
* @param repoName
|
|
106
130
|
* @param chainType
|
|
107
131
|
*/
|
|
108
|
-
const findLatestStableRelease = async (
|
|
109
|
-
|
|
132
|
+
const findLatestStableRelease = async (
|
|
133
|
+
repoName: SupportedRepo,
|
|
134
|
+
chainType: string
|
|
135
|
+
) => findLastRelease(repoName, isStableReleaseForChainType(chainType));
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Find the latest stable (that is, non-prerelease) release that tagPrefix is matched with release tag_name
|
|
139
|
+
* @param repoName
|
|
140
|
+
* @param tagPrefix
|
|
141
|
+
*/
|
|
142
|
+
const findLatestStableReleaseByPrefixTag = async (
|
|
143
|
+
repoName: SupportedRepo,
|
|
144
|
+
tagPrefix: string
|
|
145
|
+
) => findLastRelease(repoName, isStableReleaseForRegexTagType(tagPrefix));
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Find the latest release that tagPrefix is matched with release tag_name
|
|
149
|
+
* @param repoName
|
|
150
|
+
* @param tagPrefix
|
|
151
|
+
*/
|
|
152
|
+
const findLatestReleaseByPrefixTag = async (
|
|
153
|
+
repoName: SupportedRepo,
|
|
154
|
+
tagPrefix: string
|
|
155
|
+
) => findLastRelease(repoName, hasMatchedTagPrefix(tagPrefix));
|
|
110
156
|
|
|
111
157
|
export {
|
|
112
158
|
fetchReleasesPage,
|
|
113
159
|
findLastRelease,
|
|
114
160
|
findLatestRelease,
|
|
115
161
|
findLatestStableRelease,
|
|
162
|
+
findLatestStableReleaseByPrefixTag,
|
|
163
|
+
findLatestReleaseByPrefixTag,
|
|
116
164
|
getReleaseByTag,
|
|
117
165
|
hasAssetForChainType,
|
|
118
166
|
isStableReleaseForChainType,
|
|
167
|
+
isStableReleaseForRegexTagType,
|
|
168
|
+
hasMatchedTagPrefix,
|
|
119
169
|
};
|
package/lib/utils/rosen.ts
CHANGED
|
@@ -6,6 +6,13 @@
|
|
|
6
6
|
export const isValidAssetName = (chainType: string) => (assetName: string) =>
|
|
7
7
|
new RegExp(`(contracts-.+|tokensMap)-${chainType}-.+.json`).test(assetName);
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Check if an OS name is a valid supported tss OS
|
|
11
|
+
* @param OSName
|
|
12
|
+
*/
|
|
13
|
+
export const isValidOS = (OSName: string) => (releaseAssetName: string) =>
|
|
14
|
+
new RegExp(`(rosenTss-${OSName}-.+).zip`).test(releaseAssetName);
|
|
15
|
+
|
|
9
16
|
/**
|
|
10
17
|
* Remove chain type and tag from the asset name and optionally replaces them
|
|
11
18
|
* with a suffix
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@ export type PartialReleases = Partial<GithubRelease>[];
|
|
|
4
4
|
|
|
5
5
|
export const mainNetPrereleaseRelease = {
|
|
6
6
|
id: 1,
|
|
7
|
-
|
|
7
|
+
tag_name: '1',
|
|
8
8
|
prerelease: true,
|
|
9
9
|
assets: [
|
|
10
10
|
{
|
|
@@ -21,7 +21,7 @@ export const mainNetPrereleaseRelease = {
|
|
|
21
21
|
|
|
22
22
|
export const mainNetStableRelease = {
|
|
23
23
|
id: 2,
|
|
24
|
-
|
|
24
|
+
tag_name: '2',
|
|
25
25
|
prerelease: false,
|
|
26
26
|
assets: [
|
|
27
27
|
{
|
|
@@ -36,9 +36,80 @@ export const mainNetStableRelease = {
|
|
|
36
36
|
],
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
+
export const tssTag1 = {
|
|
40
|
+
id: 1,
|
|
41
|
+
tag_name: 'tss-api-1.0.0',
|
|
42
|
+
name: 'tss-api-1.0.0',
|
|
43
|
+
prerelease: false,
|
|
44
|
+
assets: [
|
|
45
|
+
{
|
|
46
|
+
name: 'rosenTss-linux-tss-api-1.0.0.zip',
|
|
47
|
+
browser_download_url:
|
|
48
|
+
'https://example.com/sign-protocols/releases/download/tss-api-1.0.0/rosenTss-linux-tss-api-1.0.0.zip',
|
|
49
|
+
} as any,
|
|
50
|
+
{
|
|
51
|
+
name: 'rosenTss-macOS-tss-api-1.0.0.zip',
|
|
52
|
+
browser_download_url:
|
|
53
|
+
'https://example.com/sign-protocols/releases/download/tss-api-1.0.0/rosenTss-macOS-tss-api-1.0.0.zip',
|
|
54
|
+
} as any,
|
|
55
|
+
{
|
|
56
|
+
name: 'rosenTss-windows-tss-api-1.0.0.zip',
|
|
57
|
+
browser_download_url:
|
|
58
|
+
'https://example.com/sign-protocols/releases/download/tss-api-1.0.0/rosenTss-windows-tss-api-1.0.0.zip',
|
|
59
|
+
} as any,
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
export const tssTag2 = {
|
|
63
|
+
id: 2,
|
|
64
|
+
tag_name: 'tss-api-2.0.0',
|
|
65
|
+
name: 'tss-api-2.0.0',
|
|
66
|
+
prerelease: false,
|
|
67
|
+
assets: [
|
|
68
|
+
{
|
|
69
|
+
name: 'rosenTss-linux-tss-api-2.0.0.zip',
|
|
70
|
+
browser_download_url:
|
|
71
|
+
'https://example.com/sign-protocols/releases/download/tss-api-2.0.0/rosenTss-linux-tss-api-2.0.0.zip',
|
|
72
|
+
} as any,
|
|
73
|
+
{
|
|
74
|
+
name: 'rosenTss-macOS-tss-api-2.0.0.zip',
|
|
75
|
+
browser_download_url:
|
|
76
|
+
'https://example.com/sign-protocols/releases/download/tss-api-2.0.0/rosenTss-macOS-tss-api-2.0.0.zip',
|
|
77
|
+
} as any,
|
|
78
|
+
{
|
|
79
|
+
name: 'rosenTss-windows-tss-api-2.0.0.zip',
|
|
80
|
+
browser_download_url:
|
|
81
|
+
'https://example.com/sign-protocols/releases/download/tss-api-2.0.0/rosenTss-windows-tss-api-2.0.0.zip',
|
|
82
|
+
} as any,
|
|
83
|
+
],
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const tssTag3PreRelease = {
|
|
87
|
+
id: 3,
|
|
88
|
+
tag_name: 'tss-api-3.0.0',
|
|
89
|
+
name: 'tss-api-3.0.0',
|
|
90
|
+
prerelease: true,
|
|
91
|
+
assets: [
|
|
92
|
+
{
|
|
93
|
+
name: 'rosenTss-linux-tss-api-3.0.0.zip',
|
|
94
|
+
browser_download_url:
|
|
95
|
+
'https://example.com/sign-protocols/releases/download/tss-api-3.0.0/rosenTss-linux-tss-api-3.0.0.zip',
|
|
96
|
+
} as any,
|
|
97
|
+
{
|
|
98
|
+
name: 'rosenTss-macOS-tss-api-3.0.0.zip',
|
|
99
|
+
browser_download_url:
|
|
100
|
+
'https://example.com/sign-protocols/releases/download/tss-api-3.0.0/rosenTss-macOS-tss-api-3.0.0.zip',
|
|
101
|
+
} as any,
|
|
102
|
+
{
|
|
103
|
+
name: 'rosenTss-windows-tss-api-3.0.0.zip',
|
|
104
|
+
browser_download_url:
|
|
105
|
+
'https://example.com/sign-protocols/releases/download/tss-api-3.0.0/rosenTss-windows-tss-api-3.0.0.zip',
|
|
106
|
+
} as any,
|
|
107
|
+
],
|
|
108
|
+
};
|
|
109
|
+
|
|
39
110
|
export const testNetPrereleaseRelease = {
|
|
40
111
|
id: 4,
|
|
41
|
-
|
|
112
|
+
tag_name: '4',
|
|
42
113
|
prerelease: true,
|
|
43
114
|
assets: [
|
|
44
115
|
{
|
|
@@ -51,7 +122,7 @@ export const testNetPrereleaseRelease = {
|
|
|
51
122
|
|
|
52
123
|
export const testNetStableRelease = {
|
|
53
124
|
id: 5,
|
|
54
|
-
|
|
125
|
+
tag_name: '5',
|
|
55
126
|
prerelease: false,
|
|
56
127
|
assets: [
|
|
57
128
|
{
|
|
@@ -62,12 +133,12 @@ export const testNetStableRelease = {
|
|
|
62
133
|
],
|
|
63
134
|
};
|
|
64
135
|
|
|
65
|
-
export const
|
|
136
|
+
export const contractReleases = [
|
|
66
137
|
mainNetPrereleaseRelease,
|
|
67
138
|
mainNetStableRelease,
|
|
68
139
|
{
|
|
69
140
|
id: 3,
|
|
70
|
-
|
|
141
|
+
tag_name: '3',
|
|
71
142
|
prerelease: false,
|
|
72
143
|
assets: [
|
|
73
144
|
{
|
|
@@ -81,7 +152,7 @@ export const releases = [
|
|
|
81
152
|
testNetStableRelease,
|
|
82
153
|
{
|
|
83
154
|
id: 6,
|
|
84
|
-
|
|
155
|
+
tag_name: '6',
|
|
85
156
|
prerelease: false,
|
|
86
157
|
assets: [
|
|
87
158
|
{
|
|
@@ -93,7 +164,7 @@ export const releases = [
|
|
|
93
164
|
},
|
|
94
165
|
{
|
|
95
166
|
id: 7,
|
|
96
|
-
|
|
167
|
+
tag_name: '7',
|
|
97
168
|
prerelease: false,
|
|
98
169
|
assets: [
|
|
99
170
|
{
|
|
@@ -105,7 +176,7 @@ export const releases = [
|
|
|
105
176
|
},
|
|
106
177
|
{
|
|
107
178
|
id: 8,
|
|
108
|
-
|
|
179
|
+
tag_name: '8',
|
|
109
180
|
prerelease: false,
|
|
110
181
|
assets: [
|
|
111
182
|
{
|
|
@@ -117,7 +188,7 @@ export const releases = [
|
|
|
117
188
|
},
|
|
118
189
|
{
|
|
119
190
|
id: 9,
|
|
120
|
-
|
|
191
|
+
tag_name: '9',
|
|
121
192
|
prerelease: false,
|
|
122
193
|
assets: [
|
|
123
194
|
{
|
|
@@ -128,3 +199,10 @@ export const releases = [
|
|
|
128
199
|
],
|
|
129
200
|
},
|
|
130
201
|
] satisfies PartialReleases;
|
|
202
|
+
|
|
203
|
+
export const tssReleases = [
|
|
204
|
+
mainNetStableRelease, // in case of sign-protocols mono repo we have other tags
|
|
205
|
+
tssTag3PreRelease,
|
|
206
|
+
tssTag2,
|
|
207
|
+
tssTag1,
|
|
208
|
+
] satisfies PartialReleases;
|
|
@@ -7,7 +7,7 @@ import { RosenAssetsDownloadError } from '../lib';
|
|
|
7
7
|
import {
|
|
8
8
|
mainNetPrereleaseRelease,
|
|
9
9
|
mainNetStableRelease,
|
|
10
|
-
|
|
10
|
+
contractReleases,
|
|
11
11
|
} from './data/octokit.data';
|
|
12
12
|
|
|
13
13
|
import { mockOctokit, mockOctokitGetReleaseByTag } from './mocks/octokit.mock';
|
|
@@ -16,7 +16,7 @@ jest.mock('download');
|
|
|
16
16
|
|
|
17
17
|
describe('downloadRosenAssets', () => {
|
|
18
18
|
beforeEach(() => {
|
|
19
|
-
mockOctokit();
|
|
19
|
+
mockOctokit(contractReleases);
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
/**
|
|
@@ -24,7 +24,7 @@ describe('downloadRosenAssets', () => {
|
|
|
24
24
|
* @dependencies
|
|
25
25
|
* - mocked Octokit
|
|
26
26
|
* @scenario
|
|
27
|
-
* - mock Octokit `listReleases` to return 9
|
|
27
|
+
* - mock Octokit `listReleases` to return 9 contractReleases
|
|
28
28
|
* - call `downloadRosenAssets` with mainnet chain type and `rosen` download
|
|
29
29
|
* directory
|
|
30
30
|
* @expected
|
|
@@ -60,7 +60,7 @@ describe('downloadRosenAssets', () => {
|
|
|
60
60
|
* @dependencies
|
|
61
61
|
* - mocked Octokit
|
|
62
62
|
* @scenario
|
|
63
|
-
* - mock Octokit `listReleases` to return 9
|
|
63
|
+
* - mock Octokit `listReleases` to return 9 contractReleases
|
|
64
64
|
* - call `downloadRosenAssets` with mainnet chain type, `rosen` download
|
|
65
65
|
* directory and including prereleases
|
|
66
66
|
* @expected
|
|
@@ -99,7 +99,7 @@ describe('downloadRosenAssets', () => {
|
|
|
99
99
|
* @dependencies
|
|
100
100
|
* - mocked Octokit
|
|
101
101
|
* @scenario
|
|
102
|
-
* - mock Octokit `listReleases` to return 9
|
|
102
|
+
* - mock Octokit `listReleases` to return 9 contractReleases
|
|
103
103
|
* - call `downloadRosenAssets` with mainnet chain type, `rosen` download
|
|
104
104
|
* directory, including prereleases and a providing a suffix
|
|
105
105
|
* @expected
|
|
@@ -141,13 +141,13 @@ describe('downloadRosenAssets', () => {
|
|
|
141
141
|
* - `download` should be called with the assets of "3" tag release
|
|
142
142
|
*/
|
|
143
143
|
it('should download a Rosen asset by tag', async () => {
|
|
144
|
-
mockOctokitGetReleaseByTag();
|
|
144
|
+
mockOctokitGetReleaseByTag(contractReleases);
|
|
145
145
|
await downloadRosenAssets('mainnet', 'rosen', {
|
|
146
146
|
tag: '3',
|
|
147
147
|
});
|
|
148
148
|
|
|
149
149
|
expect(download).toHaveBeenCalledWith(
|
|
150
|
-
|
|
150
|
+
contractReleases[2].assets[0].browser_download_url,
|
|
151
151
|
'rosen',
|
|
152
152
|
{
|
|
153
153
|
filename: 'contracts-awesomechain.json',
|
|
@@ -162,7 +162,7 @@ describe('downloadRosenAssets', () => {
|
|
|
162
162
|
* - mocked Octokit
|
|
163
163
|
* - emptied mocked download package
|
|
164
164
|
* @scenario
|
|
165
|
-
* - mock Octokit `listReleases` to return 9
|
|
165
|
+
* - mock Octokit `listReleases` to return 9 contractReleases
|
|
166
166
|
* - clear download package mock data (so that we can check calls count)
|
|
167
167
|
* - call `downloadRosenAssets` with "no-release-net" chain type and `rosen`
|
|
168
168
|
* download directory
|
|
@@ -183,7 +183,7 @@ describe('downloadRosenAssets', () => {
|
|
|
183
183
|
* - mocked Octokit
|
|
184
184
|
* - mocked download package
|
|
185
185
|
* @scenario
|
|
186
|
-
* - mock Octokit `listReleases` to return 9
|
|
186
|
+
* - mock Octokit `listReleases` to return 9 contractReleases
|
|
187
187
|
* - mock download package to throw an error
|
|
188
188
|
* - call `downloadRosenAssets` with mainnet chain type and `rosen` download
|
|
189
189
|
* directory
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import download from 'download';
|
|
2
|
+
|
|
3
|
+
import { downloadTssBinary } from '../lib';
|
|
4
|
+
import { RosenAssetsDownloadError } from '../lib';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
tssReleases,
|
|
8
|
+
tssTag1,
|
|
9
|
+
tssTag2,
|
|
10
|
+
tssTag3PreRelease,
|
|
11
|
+
} from './data/octokit.data';
|
|
12
|
+
|
|
13
|
+
import { mockOctokit, mockOctokitGetReleaseByTag } from './mocks/octokit.mock';
|
|
14
|
+
|
|
15
|
+
jest.mock('download');
|
|
16
|
+
|
|
17
|
+
describe('downloadTssBinary', () => {
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
mockOctokit(tssReleases);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @target `downloadTssBinary` should download Tss binary correctly by specific tag
|
|
24
|
+
* @dependencies
|
|
25
|
+
* - mocked Octokit
|
|
26
|
+
* - mocked Octokit GetReleaseByTag
|
|
27
|
+
* @scenario
|
|
28
|
+
* - mock Octokit `listReleases` to return tssReleases
|
|
29
|
+
* - mock Octokit GetReleaseByTag to return desired release
|
|
30
|
+
* - call `downloadTssBinary` with Windows OS name, `tss-api-1.0.0` and `rosen` download
|
|
31
|
+
* directory
|
|
32
|
+
* @expected
|
|
33
|
+
* - `download` function should be called with third asset of tss-api stable
|
|
34
|
+
* release (windows) download url and `bin` download directory
|
|
35
|
+
*/
|
|
36
|
+
it('should download Tss binary correctly with specific tag', async () => {
|
|
37
|
+
mockOctokitGetReleaseByTag(tssReleases);
|
|
38
|
+
await downloadTssBinary('bin', {
|
|
39
|
+
osName: 'windows',
|
|
40
|
+
tag: 'tss-api-1.0.0',
|
|
41
|
+
regex: false,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(download).toHaveBeenCalledWith(
|
|
45
|
+
tssTag1.assets[2].browser_download_url,
|
|
46
|
+
'bin'
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @target `downloadTssBinary` should download Tss binary correctly by
|
|
52
|
+
* prefix of a tag
|
|
53
|
+
* @dependencies
|
|
54
|
+
* - mocked Octokit
|
|
55
|
+
* @scenario
|
|
56
|
+
* - mock Octokit `listReleases` to return tssReleases
|
|
57
|
+
* - call `downloadTssBinary` with linux OS name, prefix tag `tss-api` and
|
|
58
|
+
* regex should be true
|
|
59
|
+
* @expected
|
|
60
|
+
* - `download` function should be called with first asset of tss-api stable
|
|
61
|
+
* release (linux) download url and `bin` download directory
|
|
62
|
+
*/
|
|
63
|
+
it('should download Tss binary correctly with prefix of tag', async () => {
|
|
64
|
+
await downloadTssBinary('bin', {
|
|
65
|
+
osName: 'linux',
|
|
66
|
+
tag: 'tss-api',
|
|
67
|
+
regex: true,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(download).toHaveBeenCalledWith(
|
|
71
|
+
tssTag2.assets[0].browser_download_url,
|
|
72
|
+
'bin'
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @target `downloadTssBinary` should download Tss binary correctly by
|
|
78
|
+
* prefix of a tag for an included prereleases release
|
|
79
|
+
* @dependencies
|
|
80
|
+
* - mocked Octokit
|
|
81
|
+
* @scenario
|
|
82
|
+
* - mock Octokit `listReleases` to return tssReleases
|
|
83
|
+
* - call `downloadTssBinary` with linux OS name, prefix tag `tss-api`,
|
|
84
|
+
* enable includePrereleases and regex should be true
|
|
85
|
+
* @expected
|
|
86
|
+
* - `download` function should be called with first asset of tss-api prerelease
|
|
87
|
+
* release (linux) download url and `bin` download directory
|
|
88
|
+
*/
|
|
89
|
+
it('should download prerelease Tss binary correctly with prefix of tag', async () => {
|
|
90
|
+
await downloadTssBinary('bin', {
|
|
91
|
+
osName: 'linux',
|
|
92
|
+
tag: 'tss-api',
|
|
93
|
+
regex: true,
|
|
94
|
+
includePrereleases: true,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
expect(download).toHaveBeenCalledWith(
|
|
98
|
+
tssTag3PreRelease.assets[0].browser_download_url,
|
|
99
|
+
'bin'
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @target `downloadTssBinary` should not call `download` function when no
|
|
105
|
+
* matching release is found
|
|
106
|
+
* @dependencies
|
|
107
|
+
* - mocked Octokit
|
|
108
|
+
* - emptied mocked download package
|
|
109
|
+
* @scenario
|
|
110
|
+
* - mock Octokit `listReleases` to return tssReleases
|
|
111
|
+
* - clear download package mock data (so that we can check calls count)
|
|
112
|
+
* - call `downloadTssBinary` with "no-release-tag", osName linux and `bin`
|
|
113
|
+
* download directory
|
|
114
|
+
* @expected
|
|
115
|
+
* - `download` function should not get called
|
|
116
|
+
*/
|
|
117
|
+
it('should not call `download` function when no matching release is found', async () => {
|
|
118
|
+
jest.mocked(download).mockClear();
|
|
119
|
+
|
|
120
|
+
await downloadTssBinary('bin', {
|
|
121
|
+
osName: 'linux',
|
|
122
|
+
tag: 'no-release-tag',
|
|
123
|
+
regex: true,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
expect(download).toHaveBeenCalledTimes(0);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @target `downloadTssBinary` should throw an error when an error happens
|
|
131
|
+
* @dependencies
|
|
132
|
+
* - mocked Octokit
|
|
133
|
+
* - mocked download package
|
|
134
|
+
* @scenario
|
|
135
|
+
* - mock Octokit `listReleases` to return tssReleases
|
|
136
|
+
* - mock download package to throw an error
|
|
137
|
+
* - call `downloadTssBinary` with linux OS name, prefix tag `tss-api` and
|
|
138
|
+
* regex should be true
|
|
139
|
+
* @expected
|
|
140
|
+
* - `download` function should throw `RosenAssetsDownloadError`
|
|
141
|
+
*/
|
|
142
|
+
it('should throw an error when an error happens', async () => {
|
|
143
|
+
jest.mocked(download).mockRejectedValue(new Error('Bad!'));
|
|
144
|
+
|
|
145
|
+
const downloadPromise = downloadTssBinary('bin', {
|
|
146
|
+
osName: 'linux',
|
|
147
|
+
tag: 'tss-api',
|
|
148
|
+
regex: true,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
await expect(downloadPromise).rejects.toThrow(RosenAssetsDownloadError);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Octokit } from 'octokit';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { contractReleases, PartialReleases } from '../data/octokit.data';
|
|
4
4
|
|
|
5
5
|
import { DEFAULT_RELEASES_FETCHING_PAGE_SIZE } from '../../lib/constants';
|
|
6
6
|
|
|
7
|
-
export const mockOctokit = () =>
|
|
7
|
+
export const mockOctokit = (releases: any[]) =>
|
|
8
8
|
jest.mocked(Octokit).mockImplementation(() => {
|
|
9
9
|
let page = 0;
|
|
10
10
|
return {
|
|
@@ -25,14 +25,14 @@ export const mockOctokit = () =>
|
|
|
25
25
|
/**
|
|
26
26
|
* mock `getReleaseByTag` of Octokit
|
|
27
27
|
*/
|
|
28
|
-
export const mockOctokitGetReleaseByTag = () =>
|
|
28
|
+
export const mockOctokitGetReleaseByTag = (releases: PartialReleases) =>
|
|
29
29
|
jest.mocked(Octokit).mockImplementation(() => {
|
|
30
30
|
return {
|
|
31
31
|
rest: {
|
|
32
32
|
repos: {
|
|
33
33
|
getReleaseByTag: async ({ tag }: { tag: string }) => {
|
|
34
34
|
return {
|
|
35
|
-
data: releases.find((release) => release.
|
|
35
|
+
data: releases.find((release) => release.tag_name === tag),
|
|
36
36
|
};
|
|
37
37
|
},
|
|
38
38
|
},
|