import { assertEquals } from "https://deno.land/std@0.176.0/testing/asserts.ts";
import { byteToStr, wordToStr, DisplayCode, getPixelValue } from './transmit.ts'

function orig_byteToStr(v: number) {
  return String.fromCharCode((v & 0xF) + 97, ((v >> 4) & 0xF) + 97);
}
function orig_wordToStr(v: number) {
  return byteToStr(v & 0xFF) + byteToStr((v >> 8) & 0xFF);
}

function orig_displayCode(epdInd = 22) {
  return "EPD" + String.fromCharCode(epdInd + 97) + "_"
}

function orig_getVal(p: { data: Uint8ClampedArray }, i: number) {
  if ((p.data[i]==0x00) && (p.data[i+1]==0x00)) return 0
  if ((p.data[i]==0xFF) && (p.data[i+1]==0xFF)) return 1
  if ((p.data[i]==0x7F) && (p.data[i+1]==0x7F)) return 2
  return 3
}

Deno.test(function test_byteToStr() {
  const testValues = [0,1,2,23,42,255]
  const results = testValues.map(v => byteToStr(v))
  const expected = testValues.map(v => orig_byteToStr(v))
  assertEquals(results, expected)
})

Deno.test(function test_wordToStr() {
  const testValues = [0,1,2,23,42,384]
  const results = testValues.map(v => wordToStr(v))
  const expected = testValues.map(v => orig_wordToStr(v))
  assertEquals(results, expected)
})

Deno.test(function test_displayCode() {
  assertEquals(orig_displayCode(), DisplayCode)
})

Deno.test(function test_getPixelValue() {
  // 4 pixels: white, black, black, white
  const data = new Uint8ClampedArray([
    255, 255, 255, 255,
    0, 0, 0, 255,
    0, 0, 0, 255,
    255, 255, 255, 255,
  ])
  const results = [
    getPixelValue(data, 0 << 2),
    getPixelValue(data, 1 << 2),
    getPixelValue(data, 2 << 2),
    getPixelValue(data, 3 << 2),
  ]
  const expected = [
    orig_getVal({ data }, 0 << 2),
    orig_getVal({ data }, 1 << 2),
    orig_getVal({ data }, 2 << 2),
    orig_getVal({ data }, 3 << 2),
  ]
  assertEquals(results, expected)
})
