/*
 * sabr-encoder-test.mjs — oracle test (PoC milestone 1).
 * Byte-diffs the hand-rolled VideoPlaybackAbrRequest encoder (sabr-proto.mjs,
 * the future worker port) against LuanRT googlevideo's generated encoder for
 * the SAME request object. Byte-identical output proves the field numbers,
 * wire types, field order and default-omission are all correct before any
 * add-on code is written.
 *
 *   node tools/sabr-encoder-test.mjs
 */
import { VideoPlaybackAbrRequest } from 'googlevideo/protos';
import { encodeVideoPlaybackAbrRequest } from './sabr-proto.mjs';

// Representative request exercising every field/type the worker will send.
const req = {
  clientAbrState: { clientViewportWidth: 1280, clientViewportHeight: 720, playerTimeMs: '5000' },
  selectedFormatIds: [],
  bufferedRanges: [{
    formatId: { itag: 134, lastModified: '1780253872717704' },
    startTimeMs: '1000', durationMs: '2000', startSegmentIndex: 1, endSegmentIndex: 5,
    field9: undefined, field11: undefined, field12: undefined
  }],
  playerTimeMs: '5000',
  videoPlaybackUstreamerConfig: new Uint8Array([1, 2, 3, 4, 5]),
  preferredAudioFormatIds: [{ itag: 140, lastModified: '1780253866918125' }],
  preferredVideoFormatIds: [{ itag: 134, lastModified: '1780253872717704' }],
  preferredSubtitleFormatIds: [],
  streamerContext: {
    clientInfo: { clientName: 1, clientVersion: '2.20260529.01.00', osName: 'Windows', osVersion: '10.0', deviceMake: '', deviceModel: '' },
    poToken: new Uint8Array([9, 8, 7, 6, 5, 4, 3, 2, 1]),
    sabrContexts: [], unsentSabrContexts: []
  },
  field1000: []
};

const oracle = VideoPlaybackAbrRequest.encode(req).finish();
const mine = encodeVideoPlaybackAbrRequest(req);

const hex = u8 => Buffer.from(u8).toString('hex');
console.log(`oracle (googlevideo): ${oracle.length}B`);
console.log(`hand-rolled         : ${mine.length}B`);

if (oracle.length === mine.length && oracle.every((b, i) => b === mine[i])) {
  console.log('\n✓ BYTE-IDENTICAL — hand-rolled encoder matches googlevideo');
} else {
  console.log('\n✗ MISMATCH');
  console.log('oracle:', hex(oracle));
  console.log('mine  :', hex(mine));
  let i = 0; while (i < Math.min(oracle.length, mine.length) && oracle[i] === mine[i]) i++;
  console.log(`first diff at byte ${i}: oracle=0x${(oracle[i] ?? 0).toString(16)} mine=0x${(mine[i] ?? 0).toString(16)}`);
  process.exitCode = 1;
}
