<commit_before>const test = require('tape')
const testHelper = require('../testHelper')
const timekeeper = require('timekeeper')
const proto = require('../../client/constants/proto')
const recordUtil = require('../../client/recordUtil')
const Serializer = require('../../lib/serializer')

const Record = (props) => {
  const baseProps = {
    action: proto.actions.CREATE,
    deviceId: new Uint8Array([0]),
    objectId: testHelper.newUuid()
  }
  return Object.assign({}, baseProps, props)
}
const UpdateRecord = (props) => {
  return Record(Object.assign({action: proto.actions.UPDATE}, props))
}

const timestampMs = 1480004209001
const siteProps = {
  location: 'https://www.jisho.org',
  title: 'jisho',
  customTitle: '辞書',
  lastAccessedTime: timestampMs,
  creationTime: timestampMs
}
const props = {
  bookmark: {
    isFolder: false,
    site: siteProps
  },
  historySite: siteProps,
  siteSetting: {
    fingerprintingProtection: false,
    hostPattern: 'https?://soundcloud.com',
    shieldsUp: false, // yolo
    noScript: false,
    zoomLevel: 2.5
  }
}
const updateSiteProps = {customTitle: 'a ball pit filled with plush coconuts'}

const recordBookmark = Record({objectData: 'bookmark', bookmark: props.bookmark})
const recordHistorySite = Record({objectData: 'historySite', historySite: siteProps})
const recordSiteSetting = Record({objectData: 'siteSetting', siteSetting: props.siteSetting})
const baseRecords = [recordBookmark, recordHistorySite, recordSiteSetting]

const updateBookmark = UpdateRecord({
  objectId: recordBookmark.objectId,
  objectData: 'bookmark',
  bookmark: {site: updateSiteProps}
})
const updateHistorySite = UpdateRecord({
  objectId: recordHistorySite.objectId,
  objectData: 'historySite',
  historySite: updateSiteProps
})
const updateSiteSetting = UpdateRecord({
  objectId: recordSiteSetting.objectId,
  objectData: 'siteSetting',
  siteSetting: {shieldsUp: true}
})

test('recordUtil.resolve', (t) => {
  t.plan(7)

  const forRecordsWithAction = (t, action, callback) => {
    t.plan(baseRecords.length)
    for (let record of baseRecords) {
      record.action = action
      const existingObject = Object.assign({}, record, {action: proto.actions.CREATE})
      callback(record, existingObject)
    }
  }

  t.test('CREATE, existing object -> null', (t) => {
    forRecordsWithAction(t, proto.actions.CREATE, (record, existingObject) => {
      const resolved = recordUtil.resolve(record, existingObject)
      t.equals(resolved, null, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('CREATE, no existing object -> identity', (t) => {
    forRecordsWithAction(t, proto.actions.CREATE, (record) => {
      const resolved = recordUtil.resolve(record, undefined)
      t.equals(resolved, record, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('DELETE, existing object -> identity', (t) => {
    forRecordsWithAction(t, proto.actions.DELETE, (record, existingObject) => {
      const resolved = recordUtil.resolve(record, existingObject)
      t.equals(resolved, record, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('DELETE, no existing object -> null', (t) => {
    forRecordsWithAction(t, proto.actions.DELETE, (record) => {
      const resolved = recordUtil.resolve(record, undefined)
      t.equals(resolved, null, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('UPDATE, existing object with same props -> null', (t) => {
    forRecordsWithAction(t, proto.actions.UPDATE, (record, existingObject) => {
      const resolved = recordUtil.resolve(record, existingObject)
      t.equals(resolved, null, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('UPDATE, existing object with different props -> identity', (t) => {
    t.plan(3)
    const resolvedBookmark = recordUtil.resolve(updateBookmark, recordBookmark)
    t.equals(resolvedBookmark, updateBookmark, `${t.name}: bookmark`)
    const resolvedHistorySite = recordUtil.resolve(updateHistorySite, recordHistorySite)
    t.equals(resolvedHistorySite, updateHistorySite, `${t.name}: historySite`)
    const resolvedSiteSetting = recordUtil.resolve(updateSiteSetting, recordSiteSetting)
    t.equals(resolvedSiteSetting, updateSiteSetting, `${t.name}: siteSetting`)
  })

  t.test('UPDATE, no existing object', (t) => {
    t.plan(7)
    const time = 1480000000 * 1000<commit_msg>Test: recordUtil.resolve(), existing object with reordered props<commit_after>const test = require('tape')
const testHelper = require('../testHelper')
const timekeeper = require('timekeeper')
const proto = require('../../client/constants/proto')
const recordUtil = require('../../client/recordUtil')
const Serializer = require('../../lib/serializer')

const Record = (props) => {
  const baseProps = {
    action: proto.actions.CREATE,
    deviceId: new Uint8Array([0]),
    objectId: testHelper.newUuid()
  }
  return Object.assign({}, baseProps, props)
}
const UpdateRecord = (props) => {
  return Record(Object.assign({action: proto.actions.UPDATE}, props))
}

const timestampMs = 1480004209001
const siteProps = {
  location: 'https://www.jisho.org',
  title: 'jisho',
  customTitle: '辞書',
  lastAccessedTime: timestampMs,
  creationTime: timestampMs
}
const props = {
  bookmark: {
    isFolder: false,
    site: siteProps
  },
  historySite: siteProps,
  siteSetting: {
    fingerprintingProtection: false,
    hostPattern: 'https?://soundcloud.com',
    shieldsUp: false, // yolo
    noScript: false,
    zoomLevel: 2.5
  }
}
const updateSiteProps = {customTitle: 'a ball pit filled with plush coconuts'}

const recordBookmark = Record({objectData: 'bookmark', bookmark: props.bookmark})
const recordHistorySite = Record({objectData: 'historySite', historySite: siteProps})
const recordSiteSetting = Record({objectData: 'siteSetting', siteSetting: props.siteSetting})
const baseRecords = [recordBookmark, recordHistorySite, recordSiteSetting]

const updateBookmark = UpdateRecord({
  objectId: recordBookmark.objectId,
  objectData: 'bookmark',
  bookmark: {site: updateSiteProps}
})
const updateHistorySite = UpdateRecord({
  objectId: recordHistorySite.objectId,
  objectData: 'historySite',
  historySite: updateSiteProps
})
const updateSiteSetting = UpdateRecord({
  objectId: recordSiteSetting.objectId,
  objectData: 'siteSetting',
  siteSetting: {shieldsUp: true}
})

test('recordUtil.resolve', (t) => {
  t.plan(8)

  const forRecordsWithAction = (t, action, callback) => {
    t.plan(baseRecords.length)
    for (let record of baseRecords) {
      record.action = action
      const existingObject = Object.assign({}, record, {action: proto.actions.CREATE})
      callback(record, existingObject)
    }
  }

  t.test('CREATE, existing object -> null', (t) => {
    forRecordsWithAction(t, proto.actions.CREATE, (record, existingObject) => {
      const resolved = recordUtil.resolve(record, existingObject)
      t.equals(resolved, null, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('CREATE, no existing object -> identity', (t) => {
    forRecordsWithAction(t, proto.actions.CREATE, (record) => {
      const resolved = recordUtil.resolve(record, undefined)
      t.equals(resolved, record, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('DELETE, existing object -> identity', (t) => {
    forRecordsWithAction(t, proto.actions.DELETE, (record, existingObject) => {
      const resolved = recordUtil.resolve(record, existingObject)
      t.equals(resolved, record, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('DELETE, no existing object -> null', (t) => {
    forRecordsWithAction(t, proto.actions.DELETE, (record) => {
      const resolved = recordUtil.resolve(record, undefined)
      t.equals(resolved, null, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('UPDATE, existing object with same props -> null', (t) => {
    forRecordsWithAction(t, proto.actions.UPDATE, (record, existingObject) => {
      const resolved = recordUtil.resolve(record, existingObject)
      t.equals(resolved, null, `${t.name}: ${record.objectData}`)
    })
  })

  t.test('UPDATE, existing object with same props reordered -> null', (t) => {
    t.plan(1)
    const siteSettingReordered = UpdateRecord({
      objectId: recordSiteSetting.objectId,
      objectData: 'siteSetting',
      siteSetting: {
        shieldsUp: false, // yolo
        fingerprintingProtection: false,
        hostPattern: 'https?://soundcloud.com',
        zoomLevel: 2.5,
        noScript: false
      }
    })
    const resolved = recordUtil.resolve(siteSettingReordered, recordSiteSetting)
    t.equals(resolved, null, `${t.name}: object props are reordered`)
  })

  t.test('UPDATE, existing object with different props -> identity', (t) => {
    t.plan(3)
    const resolvedBookmark = recordUtil.resolve(updateBookmark, recordBookmark)
    t.equals(resolvedBookmark, updateBookmark, `${t.name}: bookmark`)
    const resolvedHistorySite = recordUtil.resolve(updateHistorySite, recordHistorySite)
    t.equals(resolvedHistorySite, updateHistorySite, `${t.name}: historySite`)
    const resolvedSiteSetting = recordUtil.resolve(updateSiteSetting, recordSiteSetting)
    t.equals(resolvedSiteSetting, updateSiteSetting, `${t.name}: siteSetting`)
  })

  t.test('UPDATE, no existing object', (t) => {
    t.plan(7)
    const time = 1480000000 * 1000
{'fraction_non_alphanumeric': 0.14317961947758787, 'fraction_numerical': 0.00763194668386542, 'mean_word_length': 4.286363636363636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2196360', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2783, 'n_words': 633}