Skip to content

Commit 3b9bdd5

Browse files
🧪 Add tests for app-info-parser Zip class (#57)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent 0140587 commit 3b9bdd5

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

‎tests/app-info-zip.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,97 @@ suffix`,
9393
expect(info.mobileProvision.TeamName).toBe('ReactNativeCN');
9494
expect(info.icon).toBe(null);
9595
});
96+
97+
test('getEntry reads a single entry by string and regex', async () => {
98+
const zipPath = path.join(tempRoot, 'app.zip');
99+
await writeZip(zipPath, {
100+
'test.txt': 'hello world',
101+
'foo.bar': 'baz',
102+
});
103+
104+
const zip = new Zip(zipPath);
105+
106+
// By string
107+
const buf1 = await zip.getEntry('test.txt');
108+
expect(Buffer.isBuffer(buf1)).toBe(true);
109+
expect((buf1 as Buffer).toString()).toBe('hello world');
110+
111+
// By regex
112+
const buf2 = await zip.getEntry(/foo.bar/);
113+
expect(Buffer.isBuffer(buf2)).toBe(true);
114+
expect((buf2 as Buffer).toString()).toBe('baz');
115+
});
116+
117+
test('getEntry and getEntries handle blob return type', async () => {
118+
const zipPath = path.join(tempRoot, 'app.zip');
119+
await writeZip(zipPath, {
120+
'test.txt': 'hello world',
121+
});
122+
123+
const zip = new Zip(zipPath);
124+
125+
const blob1 = await zip.getEntry('test.txt', 'blob');
126+
expect(blob1).toBeInstanceOf(Blob);
127+
expect(await (blob1 as Blob).text()).toBe('hello world');
128+
129+
const buffers = await zip.getEntries(['test.txt'], 'blob');
130+
expect(buffers['test.txt']).toBeInstanceOf(Blob);
131+
expect(await (buffers['test.txt'] as Blob).text()).toBe('hello world');
132+
});
133+
134+
test('getEntryFromHarmonyApp extracts matching file', async () => {
135+
const zipPath = path.join(tempRoot, 'app.zip');
136+
await writeZip(zipPath, {
137+
'config.json': '{"harmony": true}',
138+
'other.txt': 'ignore',
139+
});
140+
141+
const zip = new Zip(zipPath);
142+
143+
const buf = await zip.getEntryFromHarmonyApp(/config.json/);
144+
expect(Buffer.isBuffer(buf)).toBe(true);
145+
expect((buf as Buffer).toString()).toBe('{"harmony": true}');
146+
});
147+
148+
test('constructor resolves path for string input', () => {
149+
const relPath = 'some/relative/path.zip';
150+
const zip = new Zip(relPath);
151+
expect(zip.file).toBe(path.resolve(relPath));
152+
});
153+
154+
test('throws error when reading entries and file is not a string', async () => {
155+
// Simulate a File object which is not a string
156+
const mockFile = new Blob(['dummy']) as File;
157+
const zip = new Zip(mockFile);
158+
159+
expect(zip.file).toBe(mockFile);
160+
161+
try {
162+
await zip.getEntry('any');
163+
expect(true).toBe(false); // Should not reach here
164+
} catch (e: any) {
165+
expect(e.message).toBe('Param error: [file] must be file path in Node.');
166+
}
167+
168+
try {
169+
await zip.getEntries(['any']);
170+
expect(true).toBe(false); // Should not reach here
171+
} catch (e: any) {
172+
expect(e.message).toBe('Param error: [file] must be file path in Node.');
173+
}
174+
175+
// getEntryFromHarmonyApp catches the error and logs to console, returning undefined
176+
const originalError = console.error;
177+
let consoleOutput = '';
178+
console.error = (_msg: string, err: any) => {
179+
consoleOutput += err.message;
180+
};
181+
const res = await zip.getEntryFromHarmonyApp(/any/);
182+
console.error = originalError;
183+
184+
expect(res).toBeUndefined();
185+
expect(consoleOutput).toBe(
186+
'Param error: [file] must be file path in Node.',
187+
);
188+
});
96189
});

0 commit comments

Comments
 (0)