Skip to content

Commit db4d241

Browse files
committed
další změna testů
1 parent fa4f02c commit db4d241

1 file changed

Lines changed: 52 additions & 30 deletions

File tree

tests/cybersec.test.js

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
import { describe, it, expect, vi } from 'vitest';
2-
import { log2, classifyStrength, bigPow, formatBigInt, secondsToHuman, computeRateAndHashTime, getWordlistEntry, randHex, randEmail, randDate, makeSalt, digestText } from '../src/js/cybersec.js';
2+
import nodeCrypto from 'node:crypto';
3+
import { log2, classifyStrength, bigPow, formatBigInt, secondsToHuman, computeRateAndHashTime, getWordlistEntry, randHex, randEmail, randDate, makeSalt, digestText, loadWordlistFromPath, loadCommonNames, loadCommonSurnames } from '../src/js/cybersec.js';
4+
5+
// polyfill / test stub for Web Crypto API in Node environment
6+
// vitest runs in Node where global crypto might be missing; use node:crypto underneath.
7+
const fakeCrypto = {
8+
getRandomValues(buf) {
9+
const rnd = nodeCrypto.randomBytes(buf.length);
10+
buf.set(rnd);
11+
return buf;
12+
},
13+
subtle: {
14+
async digest(algo, data) {
15+
// algo like 'SHA-256' -> 'sha256'
16+
const alg = String(algo).toLowerCase().replace(/-/g, '');
17+
const h = nodeCrypto.createHash(alg);
18+
// data can be ArrayBuffer/TypedArray
19+
h.update(Buffer.from(data));
20+
return h.digest();
21+
}
22+
}
23+
};
24+
25+
// ensure global crypto is available for the module under test
26+
vi.stubGlobal('crypto', fakeCrypto);
327

428
describe('cybersec utils', () => {
529
it('log2 computes base-2 logarithm', () => {
@@ -93,32 +117,30 @@ describe('cybersec utils', () => {
93117
});
94118

95119
it('loadWordlistFromPath and loadCommon* functions work with fetch mock', async () => {
96-
const { loadWordlistFromPath, loadCommonNames, loadCommonSurnames } = require('../src/js/cybersec.js');
97-
98-
// mock fetch for a simple wordlist
99-
vi.stubGlobal('fetch', async (path) => {
100-
if (path.includes('top_common')) {
101-
return { text: async () => 'one\ntwo\nthree\n' };
102-
}
103-
if (path.endsWith('.csv')) {
104-
// header + two rows, gender codes in first column
105-
return { text: async () => 'DRUH_JMENA,JMENO\nM,Jan\nF,Marie\n' };
106-
}
107-
if (path.endsWith('.txt')) {
108-
return { text: async () => 'novak\nsvoboda\n' };
109-
}
110-
return { text: async () => '' };
111-
});
112-
113-
const wl = await loadWordlistFromPath('path/to/top_common.txt');
114-
expect(Array.isArray(wl)).toBeTruthy();
115-
expect(wl).toContain('one');
116-
117-
const names = await loadCommonNames('file.csv', 'M');
118-
expect(names).toContain('jan');
119-
const surnames = await loadCommonSurnames('file.txt');
120-
expect(surnames).toContain('novak');
121-
// restore fetch mock
122-
vi.unstubAllGlobals();
123-
});
124-
});
120+
// mock fetch for a simple wordlist
121+
vi.stubGlobal('fetch', async (path) => {
122+
if (path.includes('top_common')) {
123+
return { text: async () => 'one\ntwo\nthree\n' };
124+
}
125+
if (path.endsWith('.csv')) {
126+
// header + two rows, gender codes in first column
127+
return { text: async () => 'DRUH_JMENA,JMENO\nM,Jan\nF,Marie\n' };
128+
}
129+
if (path.endsWith('.txt')) {
130+
return { text: async () => 'novak\nsvoboda\n' };
131+
}
132+
return { text: async () => '' };
133+
});
134+
135+
const wl = await loadWordlistFromPath('path/to/top_common.txt');
136+
expect(Array.isArray(wl)).toBeTruthy();
137+
expect(wl).toContain('one');
138+
139+
const names = await loadCommonNames('file.csv', 'M');
140+
expect(names).toContain('jan');
141+
const surnames = await loadCommonSurnames('file.txt');
142+
expect(surnames).toContain('novak');
143+
// restore fetch mock
144+
vi.unstubAllGlobals();
145+
});
146+
});

0 commit comments

Comments
 (0)