-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathnotices.test.ts
161 lines (147 loc) · 5.11 KB
/
notices.test.ts
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
import { after, before, describe, it } from 'node:test';
import assert from 'assert';
import { MinimalWithTypescriptIdiomTestProjectCreator } from '../test-project-setup/minimal_with_typescript_idioms.js';
import {
createTestDirectory,
deleteTestDirectory,
rootTestDir,
} from '../setup_test_directory.js';
import { TestProjectBase } from '../test-project-setup/test_project_base.js';
import { ExecaError, execa } from 'execa';
import { typedConfigurationFileFactory } from '@aws-amplify/platform-core';
import { z } from 'zod';
const betaEndpoint = 'https://beta.notices.cli.amplify.aws/notices.json';
void describe('Notices', { concurrency: false }, () => {
const projectCreator = new MinimalWithTypescriptIdiomTestProjectCreator();
let testProject: TestProjectBase;
const manifestCache = typedConfigurationFileFactory.getInstance(
'notices_metadata.json',
z.string(),
'',
);
const metadataFile = typedConfigurationFileFactory.getInstance(
'notices_acknowledgments.json',
z.string(),
'',
);
before(async () => {
await createTestDirectory(rootTestDir);
testProject = await projectCreator.createProject(rootTestDir);
});
after(async () => {
await deleteTestDirectory(rootTestDir);
});
const displaysListsAndAcknowledgesNotice = async () => {
const execaOptions = {
cwd: testProject.projectDirPath,
env: {
AMPLIFY_BACKEND_NOTICES_ENDPOINT: betaEndpoint,
},
};
// Prints unacknowledged notice in listing
let stdout = (await execa('npx', ['ampx', 'notices', 'list'], execaOptions))
.stdout;
assert.ok(
stdout.includes('This is a test notice'),
`${stdout} must include 'This is a test notice'`,
);
assert.strictEqual(
stdout.indexOf('https://github.com/aws-amplify/amplify-backend/issues/1'),
stdout.lastIndexOf(
'https://github.com/aws-amplify/amplify-backend/issues/1',
),
'Single notice must be shown only once in the output',
);
// Prints unacknowledged notice after random command
stdout = (await execa('npx', ['ampx', 'info'], execaOptions)).stdout;
assert.ok(
stdout.includes('This is a test notice'),
`${stdout} must include 'This is a test notice'`,
);
assert.strictEqual(
stdout.indexOf('https://github.com/aws-amplify/amplify-backend/issues/1'),
stdout.lastIndexOf(
'https://github.com/aws-amplify/amplify-backend/issues/1',
),
'Single notice must be shown only once in the output',
);
// Acknowledges notice
stdout = (
await execa('npx', ['ampx', 'notices', 'acknowledge', '1'], execaOptions)
).stdout;
assert.ok(
stdout.includes('has been acknowledged'),
`${stdout} must include 'has been acknowledged'`,
);
// Assert that acknowledged notices in not included in listing
stdout = (await execa('npx', ['ampx', 'notices', 'list'], execaOptions))
.stdout;
assert.ok(
!stdout.includes('This is a test notice'),
`${stdout} must not include 'This is a test notice'`,
);
// Assert that acknowledged notices in not included after random command
stdout = (await execa('npx', ['ampx', 'info'], execaOptions)).stdout;
assert.ok(
!stdout.includes('This is a test notice'),
`${stdout} must not include 'This is a test notice'`,
);
};
void describe('starting from clean state', () => {
before(async () => {
await manifestCache.delete();
await metadataFile.delete();
});
void it(
'displays, lists and acknowledges notice',
displaysListsAndAcknowledgesNotice,
);
});
void describe('starting from broken local files', () => {
before(async () => {
await manifestCache.write('broken manifest cache');
await metadataFile.write('broken acknowledgement file');
});
void it(
'displays, lists and acknowledges notice',
displaysListsAndAcknowledgesNotice,
);
});
void describe('when notices website is broken', () => {
let execaOptionWithBadEndpoint = {};
before(async () => {
await manifestCache.delete();
await metadataFile.delete();
execaOptionWithBadEndpoint = {
cwd: testProject.projectDirPath,
env: {
AMPLIFY_BACKEND_NOTICES_ENDPOINT:
'https://beta.notices.cli.amplify.aws/does_not_exist.json',
},
};
});
void it('does not crash non-notices commands', async () => {
// Executes successfully and does not print notices.
const stdout = (
await execa('npx', ['ampx', 'info'], execaOptionWithBadEndpoint)
).stdout;
assert.ok(
!stdout.includes('This is a test notice'),
`${stdout} must not include 'This is a test notice'`,
);
});
void it('prints error when listing notices', async () => {
await assert.rejects(
() =>
execa('npx', ['ampx', 'notices', 'list'], execaOptionWithBadEndpoint),
(error: ExecaError) => {
assert.ok(
typeof error.stdout === 'string' &&
error.stdout.includes('NoticeManifestFetchFault'),
);
return true;
},
);
});
});
});