Skip to content

Commit 5a93411

Browse files
committed
test: add branching to compare command
1 parent 1843379 commit 5a93411

File tree

4 files changed

+147
-15
lines changed

4 files changed

+147
-15
lines changed

src/client/errorFromLoadable.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export const errorFromLoadable = (loadable: LoadableData) => {
4343
loadable
4444
)}`;
4545

46+
// Not found (e.g. branch in project)
47+
case 404:
48+
return `Requested data not found. Please check your inputs ${addErrorDetails(loadable)}`;
49+
4650
// Rate limited
4751
case 429:
4852
return `You've been rate limited. Please try again later ${addErrorDetails(

src/utils/branch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function appendBranch(branch?: string) {
2-
return branch ? ` (branch "${branch}")` : ' (no or default branch)';
2+
return branch ? ` (branch "${branch}")` : ' (none or default branch)';
33
}

test/e2e/compare.test.ts

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { run } from './utils/run.js';
33
import { TolgeeClient } from '#cli/client/TolgeeClient.js';
44
import { PROJECT_2 } from './utils/api/project2.js';
55
import {
6+
createBranch,
7+
createKey,
68
createPak,
79
createProjectWithClient,
810
deleteProject,
@@ -34,7 +36,7 @@ const CODE_UNORDERED = fileURLToPathSlash(
3436
let client: TolgeeClient;
3537
let pak: string;
3638

37-
describe('Project 1', () => {
39+
describe('Project 2', () => {
3840
beforeEach(async () => {
3941
client = await createProjectWithClient('Project 2', PROJECT_2);
4042
pak = await createPak(client);
@@ -106,12 +108,12 @@ describe('Project 1', () => {
106108
expect(out.code).toBe(0);
107109

108110
// Test all added keys
109-
expect(out.stdout).toContain('section-content');
110-
expect(out.stdout).toContain('section-title');
111-
expect(out.stdout).toContain('copyright-notice');
112-
expect(out.stdout).toContain('privacy-policy');
113-
expect(out.stdout).toContain('terms-of-service');
114-
expect(out.stdout).toContain('welcome');
111+
expect(out.stdout).toContain('+ section-content');
112+
expect(out.stdout).toContain('+ section-title');
113+
expect(out.stdout).toContain('+ copyright-notice');
114+
expect(out.stdout).toContain('+ privacy-policy');
115+
expect(out.stdout).toContain('+ terms-of-service');
116+
expect(out.stdout).toContain('+ welcome');
115117

116118
expect(out.stdout.indexOf('section-content')).toBeLessThan(
117119
out.stdout.indexOf('section-title')
@@ -130,12 +132,12 @@ describe('Project 1', () => {
130132
);
131133

132134
// Test all removed keys
133-
expect(out.stdout).toContain('bird-name');
134-
expect(out.stdout).toContain('bird-sound');
135-
expect(out.stdout).toContain('cat-name');
136-
expect(out.stdout).toContain('cat-sound');
137-
expect(out.stdout).toContain('dog-name');
138-
expect(out.stdout).toContain('dog-sound');
135+
expect(out.stdout).toContain('- bird-name');
136+
expect(out.stdout).toContain('- bird-sound');
137+
expect(out.stdout).toContain('- cat-name');
138+
expect(out.stdout).toContain('- cat-sound');
139+
expect(out.stdout).toContain('- dog-name');
140+
expect(out.stdout).toContain('- dog-sound');
139141

140142
expect(out.stdout.indexOf('bird-name')).toBeLessThan(
141143
out.stdout.indexOf('bird-sound')
@@ -155,6 +157,78 @@ describe('Project 1', () => {
155157
}, 30e3);
156158
});
157159

160+
describe('Branching', () => {
161+
beforeEach(async () => {
162+
client = await createProjectWithClient('Project 2', PROJECT_2);
163+
pak = await createPak(client);
164+
165+
await createBranch(client, 'feature-branch');
166+
167+
await createKey(client, 'mouse-name', {
168+
branch: 'feature-branch',
169+
translations: { en: 'Mouse' },
170+
});
171+
await createKey(client, 'mouse-sound', {
172+
branch: 'feature-branch',
173+
translations: { en: 'Squeek' },
174+
});
175+
});
176+
177+
afterEach(async () => {
178+
await deleteProject(client);
179+
});
180+
181+
it('contains added keys in new branch', async () => {
182+
const out = await run(
183+
[
184+
'compare',
185+
'--api-key',
186+
pak,
187+
'--patterns',
188+
CODE_PROJECT_2_ADDED,
189+
'--branch',
190+
'feature-branch',
191+
],
192+
undefined,
193+
20e3
194+
);
195+
196+
expect(out.code).toBe(0);
197+
expect(out.stdout).toContain('is in sync');
198+
});
199+
200+
it('does not contain the added keys in main branch', async () => {
201+
const out = await run(
202+
['compare', '--api-key', pak, '--patterns', CODE_PROJECT_2_ADDED],
203+
undefined,
204+
20e3
205+
);
206+
207+
expect(out.code).toBe(0);
208+
expect(out.stdout).toContain('out of sync');
209+
expect(out.stdout).toContain('2 new keys found');
210+
}, 30e3);
211+
212+
it('fails when branch does not exist', async () => {
213+
const out = await run(
214+
[
215+
'compare',
216+
'--api-key',
217+
pak,
218+
'--patterns',
219+
CODE_PROJECT_2_COMPLETE,
220+
'--branch',
221+
'missing-branch',
222+
],
223+
undefined,
224+
20e3
225+
);
226+
227+
expect(out.code).not.toBe(0);
228+
expect(out.stderr + out.stdout).toContain('branch_not_found');
229+
}, 30e3);
230+
});
231+
158232
describe('Project 3', () => {
159233
beforeEach(async () => {
160234
client = await createProjectWithClient('Project 3', PROJECT_3);

test/e2e/utils/api/common.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export async function createProjectWithClient(
5353
) {
5454
const userToken = await userLogin();
5555
let client = createClient(userToken!);
56-
const organizations = await client.GET('/v2/organizations');
56+
const organizations = await client.GET('/v2/organizations', {
57+
params: { query: { filterCurrentUserOwner: true } },
58+
});
5759
const { languages, ...editOptions } = options ?? {};
5860

5961
const project = await client.POST('/v2/projects', {
@@ -91,6 +93,58 @@ export async function createProjectWithClient(
9193
return client;
9294
}
9395

96+
type CreateBranchOptions = {
97+
originBranchId?: number;
98+
};
99+
100+
export async function createBranch(
101+
client: TolgeeClient,
102+
name: string,
103+
options?: CreateBranchOptions
104+
) {
105+
let originBranchId = options?.originBranchId;
106+
if (!originBranchId) {
107+
const branches = await client.GET('/v2/projects/{projectId}/branches', {
108+
params: {
109+
path: { projectId: client.getProjectId() },
110+
query: { page: 0, size: 100 },
111+
},
112+
});
113+
114+
const origin = branches.data?._embedded?.branches?.find((b) => b.isDefault); ///TODO verify
115+
originBranchId = origin?.id;
116+
}
117+
118+
if (!originBranchId) {
119+
throw new Error('Default branch not found');
120+
}
121+
122+
await client.POST('/v2/projects/{projectId}/branches', {
123+
params: { path: { projectId: client.getProjectId() } },
124+
body: { name, originBranchId },
125+
});
126+
}
127+
128+
type CreateKeyOptions = Partial<
129+
Omit<components['schemas']['CreateKeyDto'], 'name'>
130+
>;
131+
132+
export async function createKey(
133+
client: TolgeeClient,
134+
name: string,
135+
options: CreateKeyOptions = {}
136+
) {
137+
const { isPlural = false, ...rest } = options;
138+
return await client.POST('/v2/projects/{projectId}/keys', {
139+
params: { path: { projectId: client.getProjectId() } },
140+
body: {
141+
name,
142+
isPlural,
143+
...rest,
144+
},
145+
});
146+
}
147+
94148
export const DEFAULT_SCOPES = [
95149
'keys.view',
96150
'translations.view',

0 commit comments

Comments
 (0)