Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to zip.js for tests #698

Merged
merged 3 commits into from
Feb 27, 2024

Conversation

toasted-nutbread
Copy link

This change updates the test code to use zip.js instead of jszip.

I ran into one curiosity while doing this: allowing createDictionaryArchiveData to generate compressed data fails for some reason, and I call this out here:

https://github.com/themoeway/yomitan/blob/e3e3b9a912a895b6cde0702e87280e8a3475d8f4/dev/dictionary-archive-util.js#L32-L35

This seems to stem from the test scripts importing directly from '@zip.js/zip.js', whereas the build application uses '@zip.js/zip.js/lib/zip.js'. If I change either of them to match the other, the tests work as expected. This change stems from #307.

@djahandarie Is there a reason why '@zip.js/zip.js/lib/zip.js' is used here vs just '@zip.js/zip.js'? Comparing the generated files after running build-libs, it does add a lot of additional code, but I haven't completely looked into what's causing the difference.

https://github.com/themoeway/yomitan/blob/4aaa9f15d97668203741c1731f15e710ae8b8294/dev/lib/zip.js#L18

For example of what happens when level is not 0, which seems to appear like the data isn't being decompressed:

Expand/collapse log
>npx vitest run test/database.test.js          

RUN  v1.2.2 yomitan

❯ test/database.test.js (10) 434ms
❯ Database (10) 433ms
    × Database invalid usage
    ❯ Invalid dictionaries (6)
    ❯ Invalid dictionary: 'invalid-dictionary1' (1)
        × Has invalid data
    ❯ Invalid dictionary: 'invalid-dictionary2' (1)
        × Has invalid data
    ❯ Invalid dictionary: 'invalid-dictionary3' (1)
        × Has invalid data
    ❯ Invalid dictionary: 'invalid-dictionary4' (1)
        × Has invalid data
    ❯ Invalid dictionary: 'invalid-dictionary5' (1)
        × Has invalid data
    ❯ Invalid dictionary: 'invalid-dictionary6' (1)
        × Has invalid data
    ❯ Database valid usage (1)
    × Import data and test
    ❯ Database cleanup (2)
    ❯ Testing cleanup method 'purge' (1)
        × Import data and test
    ❯ Testing cleanup method 'delete' (1)
        × Import data and test

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 10 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL  test/database.test.js > Database > Database invalid usage
SyntaxError: Unexpected token '�', "�V*�,�IU�R"... is not valid JSON
❯ Module.parseJson ext/js/core/json.js:29:17
    27| export function parseJson(value) {
    28|     // eslint-disable-next-line no-restricted-syntax
    29|     return JSON.parse(value);
    |                 ^
    30| }
    31| 
❯ DictionaryImporter.importDictionary ext/js/dictionary/dictionary-importer.js:94:71
❯ test/database.test.js:145:9

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/10]⎯
FAIL  test/database.test.js > Database > Invalid dictionaries > Invalid dictionary: 'invalid-dictionary1' > Has invalid data
AssertionError: expected [Function] to throw error including 'Dictionary has invalid data' but got 'Unexpected token \'M\', "M\ufffd1\n\u…'

- Expected
+ Received

- Dictionary has invalid data
+ Unexpected token 'M', "M�1
+ �0►Dѫ�"... is not valid JSON

❯ test/database.test.js:170:17


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/10]⎯
FAIL  test/database.test.js > Database > Invalid dictionaries > Invalid dictionary: 'invalid-dictionary2' > Has invalid data
AssertionError: expected [Function] to throw error including 'Dictionary has invalid data' but got 'Unexpected token \'\u0015\', "\u0015\…'

- Expected
+ Received

- Dictionary has invalid data
+ Unexpected token '§', "§�1♫�0♀F�X"... is not valid JSON

❯ test/database.test.js:170:17


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/10]⎯
FAIL  test/database.test.js > Database > Invalid dictionaries > Invalid dictionary: 'invalid-dictionary3' > Has invalid data
FAIL  test/database.test.js > Database > Invalid dictionaries > Invalid dictionary: 'invalid-dictionary4' > Has invalid data
FAIL  test/database.test.js > Database > Invalid dictionaries > Invalid dictionary: 'invalid-dictionary5' > Has invalid data
FAIL  test/database.test.js > Database > Invalid dictionaries > Invalid dictionary: 'invalid-dictionary6' > Has invalid data
AssertionError: expected [Function] to throw error including 'Dictionary has invalid data' but got 'Unexpected token \'M\', "M\ufffd1\u00…'

- Expected
+ Received

- Dictionary has invalid data
+ Unexpected token 'M', "M�1♫�0♀F�X"... is not valid JSON

❯ test/database.test.js:170:17


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/10]⎯
FAIL  test/database.test.js > Database > Database valid usage > Import data and test
SyntaxError: Unexpected token '�', "�V*�,�IU�R"... is not valid JSON
❯ Module.parseJson ext/js/core/json.js:29:17
    27| export function parseJson(value) {
    28|     // eslint-disable-next-line no-restricted-syntax
    29|     return JSON.parse(value);
    |                 ^
    30| }
    31| 
❯ DictionaryImporter.importDictionary ext/js/dictionary/dictionary-importer.js:94:71
❯ test/database.test.js:198:86

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/10]⎯
FAIL  test/database.test.js > Database > Database cleanup > Testing cleanup method 'purge' > Import data and test
FAIL  test/database.test.js > Database > Database cleanup > Testing cleanup method 'delete' > Import data and test
SyntaxError: Unexpected token '�', "�V*�,�IU�R"... is not valid JSON
❯ Module.parseJson ext/js/core/json.js:29:17
    27| export function parseJson(value) {
    28|     // eslint-disable-next-line no-restricted-syntax
    29|     return JSON.parse(value);
    |                 ^
    30| }
    31|
❯ DictionaryImporter.importDictionary ext/js/dictionary/dictionary-importer.js:94:71
❯ test/database.test.js:322:17

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/10]⎯
Test Files  1 failed (1)
    Tests  10 failed (10)
Start at  11:27:25
Duration  1.23s (transform 369ms, setup 0ms, collect 498ms, tests 434ms, environment 0ms, prepare 90ms)

Copy link

github-actions bot commented Feb 17, 2024

✔️ No visual differences introduced by this PR.

View Playwright Report (note: open the "playwright-report" artifact)

@djahandarie
Copy link
Collaborator

Good question! My memory is fuzzy, but I recall that I was testing a couple different options from https://github.com/gildas-lormeau/zip.js/tree/master/dist#built-scripts-of-zipjs, which is why I was importing stuff from within lib/. I can't remember if there there is a reason I left it as @zip.js/zip.js/lib/zip.js, but given that it's seemingly behaving differently from '@zip.js/zip.js I guess maybe there was some reason. Sorry I don't have anything more to add. (I gave up switching the node stuff to zip.js, so you've made it farther than I have in debugging how to get it working.)

@djahandarie
Copy link
Collaborator

@toasted-nutbread I guess it's okay to merge this regardless of the above concerns? As long as there is no risk of this breaking loading of actually compressed dictionaries in the extension somehow.

@toasted-nutbread
Copy link
Author

So I think I have figured out why this is/was done, and I've updated the comments accordingly. It seems to be in order to only import the subset of functionality that the extension application needs, namely decompression and processing via web workers. Since this does not work in node, it tries to fall back, but it has nothing to fall back to since the node functionality isn't present, so it silently returns the compressed data.

For posterity, here's some code I was using for testing. This doesn't exhibit the issue since it imports all of the node stuff when it is writing the zip, but the two functions can be used in successive runs and a file on disk.

/**
 * @param {string} fileName
 * @param {unknown} json
 * @param {number} level
 * @returns {Promise<ArrayBuffer>}
 */
async function createZipData(fileName, json, level) {
    const {BlobWriter, TextReader, ZipWriter} = await import('@zip.js/zip.js');
    const zipFileWriter = new BlobWriter();
    const zipWriter = new ZipWriter(zipFileWriter, {level});
    await zipWriter.add(fileName, new TextReader(JSON.stringify(json, null, 0)));
    const blob = await zipWriter.close();
    return await blob.arrayBuffer();
}

/**
 * @param {string} fileName
 * @param {ArrayBuffer} data
 * @returns {Promise<unknown>}
 */
async function readZipData(fileName, data) {
    const {Uint8ArrayReader, ZipReader, TextWriter} = await import('@zip.js/zip.js/lib/zip.js');
    const zipFileReader = new Uint8ArrayReader(new Uint8Array(data));
    const zipReader = new ZipReader(zipFileReader);
    const entries = await zipReader.getEntries();
    const entry = entries.find((item) => item.filename === fileName);
    return await entry.getData(new TextWriter());
}

/** */
async function main() {
    const fileName = 'index.json';
    const json = {test: 'value'};
    const data = await createZipData(fileName, json, 1);
    const json2 = await readZipData(fileName, data);
    console.log(json, json2);
}

await main();

@djahandarie djahandarie added this pull request to the merge queue Feb 27, 2024
Merged via the queue into yomidevs:master with commit c4fea22 Feb 27, 2024
10 checks passed
@Casheeew Casheeew added area/dependencies The issue or PR is related to dependencies kind/meta The issue or PR is meta and removed area/dependencies The issue or PR is related to dependencies labels Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dependencies The issue or PR is related to dependencies kind/meta The issue or PR is meta
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants