'use strict'

var test = require('tap').test

const { isStale, resOp, termOp, putOps } = require('../lib/level')

var DIV = '\udbff\udfff'

test('isStale', function (t) {
  t.ok(!isStale(Date.now(), 1), 'should not be stale')
  t.ok(!isStale(Date.now() - 0.9, 1), 'should not be stale')
  t.ok(isStale(Date.now() - 2, 1), 'should be stale')
  t.end()
})

test('resOp', function (t) {
  t.plan(1)
  t.ok(!!JSON.parse(resOp({ guid: 0 }).value).ts)
  t.end()
})

function str (o) {
  return JSON.stringify(o)
}

test('termOp', function (t) {
  var keys = [
    ['fnb', 'res', '12'].join(DIV),
    ['fnb', 'res', '34'].join(DIV),
    ['fnb', 'res', '56'].join(DIV)
  ]
  var now = Date.now()
  var wanted = [
    { type: 'put',
      key: ['fnb', 'trm', 'abc'].join(DIV),
      value: str([now].concat(keys))
    }
  ]
  t.plan(1)
  ;[
    termOp('abc', keys, now)
  ].forEach(function (found, i) {
    t.deepEqual(found, wanted[i])
  })
  t.end()
})

test('putOps', function (t) {
  var now = Date.now()
  var results = [
    { guid: 12, ts: now },
    { guid: 34, ts: now },
    { guid: 56, ts: now }
  ]
  var keys = [
    ['fnb', 'res', '12'].join(DIV),
    ['fnb', 'res', '34'].join(DIV),
    ['fnb', 'res', '56'].join(DIV)
  ]
  var wanted = [
    { type: 'put', key: keys[0], value: str(results[0]) },
    { type: 'put', key: keys[1], value: str(results[1]) },
    { type: 'put', key: keys[2], value: str(results[2]) },
    { type: 'put',
      key: ['fnb', 'trm', 'abc'].join(DIV),
      value: str([now].concat(keys)) }
  ]
  var found = putOps('abc', results, now)
  t.plan(1)
  t.deepEqual(found, wanted)
  t.end()
})

'use strict'

var test = require('tap').test

const { isStale, resOp, termOp, putOps } = require('../lib/level')

var DIV = '\udbff\udfff'

test('isStale', function (t) {
  t.ok(!isStale(Date.now(), 1), 'should not be stale')
  t.ok(isStale(Date.now() - 2, 1), 'should be stale')
  t.end()
})

test('resOp', function (t) {
  t.plan(1)
  t.ok(!!JSON.parse(resOp({ guid: 0 }).value).ts)
  t.end()
})

function str (o) {
  return JSON.stringify(o)
}

test('termOp', function (t) {
  var keys = [
    ['fnb', 'res', '12'].join(DIV),
    ['fnb', 'res', '34'].join(DIV),
    ['fnb', 'res', '56'].join(DIV)
  ]
  var now = Date.now()
  var wanted = [
    { type: 'put',
      key: ['fnb', 'trm', 'abc'].join(DIV),
      value: str([now].concat(keys))
    }
  ]
  t.plan(1)
  ;[
    termOp('abc', keys, now)
  ].forEach(function (found, i) {
    t.deepEqual(found, wanted[i])
  })
  t.end()
})

test('putOps', function (t) {
  var now = Date.now()
  var results = [
    { guid: 12, ts: now },
    { guid: 34, ts: now },
    { guid: 56, ts: now }
  ]
  var keys = [
    ['fnb', 'res', '12'].join(DIV),
    ['fnb', 'res', '34'].join(DIV),
    ['fnb', 'res', '56'].join(DIV)
  ]
  var wanted = [
    { type: 'put', key: keys[0], value: str(results[0]) },
    { type: 'put', key: keys[1], value: str(results[1]) },
    { type: 'put', key: keys[2], value: str(results[2]) },
    { type: 'put',
      key: ['fnb', 'trm', 'abc'].join(DIV),
      value: str([now].concat(keys)) }
  ]
  var found = putOps('abc', results, now)
  t.plan(1)
  t.deepEqual(found, wanted)
  t.end()
})

Remove edge case