<commit_before>/* @flow */

import type fs from 'fs-extra'
import type { Metadata, MetadataSidesInfo } from '../../../../core/metadata'

const { maxDate } = require('../../../../core/timestamp')
const Pouch = require('../../../../core/pouch')

module.exports = class BaseMetadataBuilder {
  pouch: ?Pouch
  opts: {
    path: string,
    ino?: number,
    updated_at?: string|Date,
    trashed?: true,
    sides: MetadataSidesInfo
  }

  constructor (pouch: ?Pouch) {
    this.pouch = pouch
    this.opts = {
      path: 'foo',
      sides: {}
    }
  }

  ino (ino: number): this {
    this.opts.ino = ino
    return this
  }

  stats ({ino, mtime, ctime}: fs.Stats): this {
    return this.ino(ino).updatedAt(maxDate(mtime, ctime))
  }

  path (path: string): this {
    this.opts.path = path
    return this
  }

  trashed (): this {
    this.opts.trashed = true
    return this
  }

  updatedAt (date: Date): this {
    date = new Date(date)
    date.setMilliseconds(0)
    this.opts.updated_at = date.toISOString()
    return this
  }

  upToDate (): this {
    this.opts.sides = {local: 1, remote: 1}
    return this
  }

  notUpToDate (): this {
    this.opts.sides = {remote: 1}
    return this
  }

  build (): Metadata {
    throw new Error('BaseMetadataBuilder#build() not implemented')
  }

  async create (): Promise<Metadata> {
    if (this.pouch == null) {
      throw new Error('Cannot create dir metadata without Pouch')
    }
    const doc = this.build()
    // $FlowFixMe
    await this.pouch.put(doc)
    return doc
  }
}<commit_msg>test/support/builders/metadata: Make sure created docs have a rev<commit_after>/* @flow */

import type fs from 'fs-extra'
import type { Metadata, MetadataSidesInfo } from '../../../../core/metadata'

const { maxDate } = require('../../../../core/timestamp')
const Pouch = require('../../../../core/pouch')

module.exports = class BaseMetadataBuilder {
  pouch: ?Pouch
  opts: {
    path: string,
    ino?: number,
    updated_at?: string|Date,
    trashed?: true,
    sides: MetadataSidesInfo
  }

  constructor (pouch: ?Pouch) {
    this.pouch = pouch
    this.opts = {
      path: 'foo',
      sides: {}
    }
  }

  ino (ino: number): this {
    this.opts.ino = ino
    return this
  }

  stats ({ino, mtime, ctime}: fs.Stats): this {
    return this.ino(ino).updatedAt(maxDate(mtime, ctime))
  }

  path (path: string): this {
    this.opts.path = path
    return this
  }

  trashed (): this {
    this.opts.trashed = true
    return this
  }

  updatedAt (date: Date): this {
    date = new Date(date)
    date.setMilliseconds(0)
    this.opts.updated_at = date.toISOString()
    return this
  }

  upToDate (): this {
    this.opts.sides = {local: 1, remote: 1}
    return this
  }

  notUpToDate (): this {
    this.opts.sides = {remote: 1}
    return this
  }

  build (): Metadata {
    throw new Error('BaseMetadataBuilder#build() not implemented')
  }

  async create (): Promise<Metadata> {
    if (this.pouch == null) {
      throw new Error('Cannot create dir metadata without Pouch')
    }
    const doc = this.build()
    // $FlowFixMe
    const {rev} = await this.pouch.put(doc)
    doc._rev = rev
    return doc
  }
}
{'fraction_non_alphanumeric': 0.15145813734713076, 'fraction_numerical': 0.002508623392913139, 'mean_word_length': 2.459869848156182, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '511220', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1081, 'n_words': 275}