11cb0ef41Sopenharmony_ci// Flags: --expose-internals --no-warnings
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciimport '../common/index.mjs';
41cb0ef41Sopenharmony_ciimport { describe, it, beforeEach } from 'node:test';
51cb0ef41Sopenharmony_ciimport assert from 'node:assert';
61cb0ef41Sopenharmony_ciimport { finished } from 'node:stream/promises';
71cb0ef41Sopenharmony_ciimport { DefaultSerializer } from 'node:v8';
81cb0ef41Sopenharmony_ciimport serializer from 'internal/test_runner/reporter/v8-serializer';
91cb0ef41Sopenharmony_ciimport runner from 'internal/test_runner/runner';
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciasync function toArray(chunks) {
121cb0ef41Sopenharmony_ci  const arr = [];
131cb0ef41Sopenharmony_ci  for await (const i of chunks) arr.push(i);
141cb0ef41Sopenharmony_ci  return arr;
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst chunks = await toArray(serializer([
181cb0ef41Sopenharmony_ci  { type: 'test:diagnostic', data: { nesting: 0, details: {}, message: 'diagnostic' } },
191cb0ef41Sopenharmony_ci]));
201cb0ef41Sopenharmony_ciconst defaultSerializer = new DefaultSerializer();
211cb0ef41Sopenharmony_cidefaultSerializer.writeHeader();
221cb0ef41Sopenharmony_ciconst headerLength = defaultSerializer.releaseBuffer().length;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cidescribe('v8 deserializer', () => {
251cb0ef41Sopenharmony_ci  let fileTest;
261cb0ef41Sopenharmony_ci  let reported;
271cb0ef41Sopenharmony_ci  beforeEach(() => {
281cb0ef41Sopenharmony_ci    reported = [];
291cb0ef41Sopenharmony_ci    fileTest = new runner.FileTest({ name: 'filetest' });
301cb0ef41Sopenharmony_ci    fileTest.reporter.on('data', (data) => reported.push(data));
311cb0ef41Sopenharmony_ci    assert(fileTest.isClearToSend());
321cb0ef41Sopenharmony_ci  });
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  async function collectReported(chunks) {
351cb0ef41Sopenharmony_ci    chunks.forEach((chunk) => fileTest.parseMessage(chunk));
361cb0ef41Sopenharmony_ci    fileTest.drain();
371cb0ef41Sopenharmony_ci    fileTest.reporter.end();
381cb0ef41Sopenharmony_ci    await finished(fileTest.reporter);
391cb0ef41Sopenharmony_ci    return reported;
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  it('should do nothing when no chunks', async () => {
431cb0ef41Sopenharmony_ci    const reported = await collectReported([]);
441cb0ef41Sopenharmony_ci    assert.deepStrictEqual(reported, []);
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  it('should deserialize a chunk with no serialization', async () => {
481cb0ef41Sopenharmony_ci    const reported = await collectReported([Buffer.from('unknown')]);
491cb0ef41Sopenharmony_ci    assert.deepStrictEqual(reported, [
501cb0ef41Sopenharmony_ci      { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
511cb0ef41Sopenharmony_ci    ]);
521cb0ef41Sopenharmony_ci  });
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  it('should deserialize a serialized chunk', async () => {
551cb0ef41Sopenharmony_ci    const reported = await collectReported(chunks);
561cb0ef41Sopenharmony_ci    assert.deepStrictEqual(reported, [
571cb0ef41Sopenharmony_ci      { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
581cb0ef41Sopenharmony_ci    ]);
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  it('should deserialize a serialized chunk after non-serialized chunk', async () => {
621cb0ef41Sopenharmony_ci    const reported = await collectReported([Buffer.concat([Buffer.from('unknown'), ...chunks])]);
631cb0ef41Sopenharmony_ci    assert.deepStrictEqual(reported, [
641cb0ef41Sopenharmony_ci      { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
651cb0ef41Sopenharmony_ci      { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
661cb0ef41Sopenharmony_ci    ]);
671cb0ef41Sopenharmony_ci  });
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  it('should deserialize a serialized chunk before non-serialized output', async () => {
701cb0ef41Sopenharmony_ci    const reported = await collectReported([Buffer.concat([ ...chunks, Buffer.from('unknown')])]);
711cb0ef41Sopenharmony_ci    assert.deepStrictEqual(reported, [
721cb0ef41Sopenharmony_ci      { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
731cb0ef41Sopenharmony_ci      { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
741cb0ef41Sopenharmony_ci    ]);
751cb0ef41Sopenharmony_ci  });
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  const headerPosition = headerLength * 2 + 4;
781cb0ef41Sopenharmony_ci  for (let i = 0; i < headerPosition + 5; i++) {
791cb0ef41Sopenharmony_ci    const message = `should deserialize a serialized message split into two chunks {...${i},${i + 1}...}`;
801cb0ef41Sopenharmony_ci    it(message, async () => {
811cb0ef41Sopenharmony_ci      const data = chunks[0];
821cb0ef41Sopenharmony_ci      const reported = await collectReported([data.subarray(0, i), data.subarray(i)]);
831cb0ef41Sopenharmony_ci      assert.deepStrictEqual(reported, [
841cb0ef41Sopenharmony_ci        { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
851cb0ef41Sopenharmony_ci      ]);
861cb0ef41Sopenharmony_ci    });
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci    it(`${message} wrapped by non-serialized data`, async () => {
891cb0ef41Sopenharmony_ci      const data = chunks[0];
901cb0ef41Sopenharmony_ci      const reported = await collectReported([
911cb0ef41Sopenharmony_ci        Buffer.concat([Buffer.from('unknown'), data.subarray(0, i)]),
921cb0ef41Sopenharmony_ci        Buffer.concat([data.subarray(i), Buffer.from('unknown')]),
931cb0ef41Sopenharmony_ci      ]);
941cb0ef41Sopenharmony_ci      assert.deepStrictEqual(reported, [
951cb0ef41Sopenharmony_ci        { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
961cb0ef41Sopenharmony_ci        { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' },
971cb0ef41Sopenharmony_ci        { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' },
981cb0ef41Sopenharmony_ci      ]);
991cb0ef41Sopenharmony_ci    }
1001cb0ef41Sopenharmony_ci    );
1011cb0ef41Sopenharmony_ci  }
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci});
104