<commit_before>import { BranchPruner } from '../../src/lib/stores/helpers/branch-pruner'
import { Repository } from '../../src/models/repository'
import { GitStoreCache } from '../../src/lib/stores/git-store-cache'
import { RepositoriesStore } from '../../src/lib/stores'
import {
  IRepositoryStateCache,
  getInitialRepositoryState,
} from '../../src/lib/stores/repository-state-cache'
import { setupFixtureRepository } from '../helpers/repositories'
import { shell } from '../helpers/test-app-shell'
import { TestRepositoriesDatabase } from '../helpers/databases'
import { IGitHubUser } from '../../src/lib/databases'
import { GitProcess } from 'dugite'
import { Branch } from '../../src/models/branch'
import { GitHubRepository } from '../../src/models/github-repository'
import { Owner } from '../../src/models/owner'
jest.useFakeTimers()
jest.mock('../../src/lib/stores/repositories-store')

describe('BranchPruner', () => {
  const onGitStoreUpdated = () => {}
  const onDidLoadNewCommits = () => {}
  const onDidError = () => {}

  let gitStoreCache: GitStoreCache
  let repositoriesStore: RepositoriesStore
  let repositoriesStateCache: IRepositoryStateCache
  let onPruneCompleted: jest.Mock<
    (
      repository: Repository,
      prunedBranches: ReadonlyArray<Branch>
    ) => Promise<void>
  >
  let realDateNow: () => number

  function mockDateNow(date: Date) {
    const stub = jest.fn(() => date.getTime())
    global.Date.now = stub
    return stub
  }

  beforeEach(async () => {
    gitStoreCache = new GitStoreCache(
      shell,
      onGitStoreUpdated,
      onDidLoadNewCommits,
      onDidError
    )

    const repositoriesDb = new TestRepositoriesDatabase()
    await repositoriesDb.reset()
    repositoriesStore = new RepositoriesStore(repositoriesDb)

    repositoriesStateCache = {
      get: (r: Repository) => getInitialRepositoryState(),
    }

    onPruneCompleted = jest.fn(
      () => (repository: Repository, prunedBranches: ReadonlyArray<Branch>) =>
        Promise.resolve()
    )

    realDateNow = Date.now.bind(global.Date)
  afterEach(() => {
    global.Date.now = realDateNow
  })

  it('Does nothing on non GitHub repositories', async () => {
    const path = await setupFixtureRepository('branch-prune-cases')
    const repository = new Repository(path, 0, null, false)
    const branchPruner = new BranchPruner(
      repository,
      gitStoreCache,
      repositoriesStore,
      repositoriesStateCache,
      onPruneCompleted
    )

    let gitOutput = await GitProcess.exec(['branch'], repository.path)<commit_msg>Print out some debugging info<commit_after>import { BranchPruner } from '../../src/lib/stores/helpers/branch-pruner'
import { Repository } from '../../src/models/repository'
import { GitStoreCache } from '../../src/lib/stores/git-store-cache'
import { RepositoriesStore } from '../../src/lib/stores'
import {
  IRepositoryStateCache,
  getInitialRepositoryState,
} from '../../src/lib/stores/repository-state-cache'
import { setupFixtureRepository } from '../helpers/repositories'
import { shell } from '../helpers/test-app-shell'
import { TestRepositoriesDatabase } from '../helpers/databases'
import { IGitHubUser } from '../../src/lib/databases'
import { GitProcess } from 'dugite'
import { Branch } from '../../src/models/branch'
import { GitHubRepository } from '../../src/models/github-repository'
import { Owner } from '../../src/models/owner'
jest.useFakeTimers()
jest.mock('../../src/lib/stores/repositories-store')

describe('BranchPruner', () => {
  const onGitStoreUpdated = () => {}
  const onDidLoadNewCommits = () => {}
  const onDidError = () => {}

  let gitStoreCache: GitStoreCache
  let repositoriesStore: RepositoriesStore
  let repositoriesStateCache: IRepositoryStateCache
  let onPruneCompleted: jest.Mock<
    (
      repository: Repository,
      prunedBranches: ReadonlyArray<Branch>
    ) => Promise<void>
  >
  let realDateNow: () => number

  function mockDateNow(date: Date) {
    const stub = jest.fn(() => date.getTime())
    global.Date.now = stub
    return stub
  }

  beforeEach(async () => {
    gitStoreCache = new GitStoreCache(
      shell,
      onGitStoreUpdated,
      onDidLoadNewCommits,
      onDidError
    )

    const repositoriesDb = new TestRepositoriesDatabase()
    await repositoriesDb.reset()
    repositoriesStore = new RepositoriesStore(repositoriesDb)

    repositoriesStateCache = {
      get: (r: Repository) => getInitialRepositoryState(),
    }

    onPruneCompleted = jest.fn(
      () => (repository: Repository, prunedBranches: ReadonlyArray<Branch>) => {
        console.log('repository', repository)
        console.log('prunedBranches', prunedBranches)
        return Promise.resolve()
      }
    )

    realDateNow = Date.now.bind(global.Date)
  })

  afterEach(() => {
    global.Date.now = realDateNow
  })

  it('Does nothing on non GitHub repositories', async () => {
    const path = await setupFixtureRepository('branch-prune-cases')
    const repository = new Repository(path, 0, null, false)
    const branchPruner = new BranchPruner(
      repository,
      gitStoreCache,
      repositoriesStore,
      repositoriesStateCache,
      onPruneCompleted
    )

    let gitOutput = await GitProcess.exec(['branch'], repository.path)
{'fraction_non_alphanumeric': 0.13644115974985788, 'fraction_numerical': 0.00037900322152738296, 'mean_word_length': 3.4130434782608696, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, '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': '6521457', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1559, 'n_words': 326}