<commit_before>import { dangerRepresentationForPath, dangerRunForRules, dslTypeForEvent, RunFeedback, RunType } from "../danger_run"

describe("for ping", () => {
  it("returns an action when ping is in the rules", () => {
    const rules = { ping: "dangerfile.js" }
    expect(dangerRunForRules("ping", null, rules)).toEqual([
      {
        action: null,
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.import,
        event: "ping",
        feedback: RunFeedback.silent,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns nothing when ping is not in the rules", () => {
    const rules = {}
    expect(dangerRunForRules("ping", null, rules)).toEqual([])
  })
})

describe("for PRs", () => {
  it("returns a PR when PR is in the rules", () => {
    const rules = { pull_request: "dangerfile.js" }
    expect(dangerRunForRules("pull_request", "created", rules)).toEqual([
      {
        action: "created",
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.pr,
        event: "pull_request",
        feedback: RunFeedback.commentable,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns two events when ", () => {
    const rules = { pull_request: ["dangerfile.js", "anotherdangerfile.ts"] }
    expect(dangerRunForRules("pull_request", "created", rules).map(r => r.dangerfilePath)).toEqual([
      "dangerfile.js",
      "anotherdangerfile.ts",
    ])
  })

  // Same semantics
  it("returns a PR run when all sub events are globbed in the rules", () => {
    const rules = { "pull_request.*": "dangerfile.js" }
    expect(dangerRunForRules("pull_request", "updated", rules)).toEqual([
      {
        action: "updated",
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.pr,
        event: "pull_request",
        feedback: RunFeedback.commentable,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns null when you only ask for a specific action", () => {
    const rules = { "pull_request.created": "dangerfile.js" }
    expect(dangerRunForRules("pull_request", "updated", rules)).toEqual([])
  })

  it("returns a PR run when all sub events are globbed in the rules", () => {
    const rules = { "pull_request.deleted": "dangerfile.js" }
    expect(dangerRunForRules("pull_request", "deleted", rules)).toEqual([
      {
        action: "deleted",
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.pr,
        event: "pull_request",
        feedback: RunFeedback.commentable,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns many runs when there are mutliple potential matches", () => {
    const rules = {
      issue: "dangerfile.js",
      pull_request: "dangerfile.js",
      "pull_request.*": "dangerfile.js",
      "pull_request.updated": "dangerfile.js",
    }
    expect(dangerRunForRules("pull_request", "updated", rules).length).toEqual(3)
  })
})

describe("dangerRepresentationforPath", () => {
  it("returns just the path with master and no repo with just a path", () => {
    const path = "dangerfile.ts"
    expect(dangerRepresentationForPath(path)).toEqual({
      branch: "master",<commit_msg>Add tests for inline rules<commit_after>import { dangerRepresentationForPath, dangerRunForRules, dslTypeForEvent, RunFeedback, RunType } from "../danger_run"

describe("for ping", () => {
  it("returns an action when ping is in the rules", () => {
    const rules = { ping: "dangerfile.js" }
    expect(dangerRunForRules("ping", null, rules)).toEqual([
      {
        action: null,
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.import,
        event: "ping",
        feedback: RunFeedback.silent,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns nothing when ping is not in the rules", () => {
    const rules = {}
    expect(dangerRunForRules("ping", null, rules)).toEqual([])
  })
})

describe("for PRs", () => {
  it("returns a PR when PR is in the rules", () => {
    const rules = { pull_request: "dangerfile.js" }
    expect(dangerRunForRules("pull_request", "created", rules)).toEqual([
      {
        action: "created",
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.pr,
        event: "pull_request",
        feedback: RunFeedback.commentable,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns two events when rule contains two files", () => {
    const rules = { pull_request: ["dangerfile.js", "anotherdangerfile.ts"] }
    expect(dangerRunForRules("pull_request", "created", rules).map(r => r.dangerfilePath)).toEqual([
      "dangerfile.js",
      "anotherdangerfile.ts",
    ])
  })

  // Same semantics
  it("returns a PR run when all sub events are globbed in the rules", () => {
    const rules = { "pull_request.*": "dangerfile.js" }
    expect(dangerRunForRules("pull_request", "updated", rules)).toEqual([
      {
        action: "updated",
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.pr,
        event: "pull_request",
        feedback: RunFeedback.commentable,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns null when you only ask for a specific action", () => {
    const rules = { "pull_request.created": "dangerfile.js" }
    expect(dangerRunForRules("pull_request", "updated", rules)).toEqual([])
  })

  it("returns a PR run when event contains action suffix", () => {
    const rules = { "pull_request.deleted": "dangerfile.js" }
    expect(dangerRunForRules("pull_request", "deleted", rules)).toEqual([
      {
        action: "deleted",
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.pr,
        event: "pull_request",
        feedback: RunFeedback.commentable,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns many runs when there are mutliple potential matches", () => {
    const rules = {
      issue: "dangerfile.js",
      pull_request: "dangerfile.js",
      "pull_request.*": "dangerfile.js",
      "pull_request.updated": "dangerfile.js",
    }
    expect(dangerRunForRules("pull_request", "updated", rules).length).toEqual(3)
  })

  it("returns a PR when multiple rules are declared inline", () => {
    const rules = {
      "issue, pull_request": "dangerfile.js",
    }
    expect(dangerRunForRules("pull_request", "created", rules)).toEqual([
      {
        action: "created",
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.pr,
        event: "pull_request",
        feedback: RunFeedback.commentable,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns null when no multi inline rules match", () => {
    const rules = {
      "issue.created, pull_request.closed": "dangerfile.js",
    }
    expect(dangerRunForRules("pull_request", "created", rules)).toEqual([])
  })

  it("returns a PR when PR is in the rules and there are multi inline rules", () => {
    const rules = {
      "issue.created, issue.closed": "dangerfile.js",
      pull_request: "dangerfile.js",
    }
    expect(dangerRunForRules("pull_request", "created", rules)).toEqual([
      {
        action: "created",
        branch: "master",
        dangerfilePath: "dangerfile.js",
        dslType: RunType.pr,
        event: "pull_request",
        feedback: RunFeedback.commentable,
        referenceString: "dangerfile.js",
        repoSlug: undefined,
      },
    ])
  })

  it("returns one run when there are many potential matches with inline rules", () => {
    const rules = {
      issue: "dangerfile.js",
      "pull_request, pull_request.*, pull_request.updated": "dangerfile.js",
    }
    expect(dangerRunForRules("pull_request", "updated", rules).length).toEqual(1)
  })
})

describe("dangerRepresentationforPath", () => {
  it("returns just the path with master and no repo with just a path", () => {
    const path = "dangerfile.ts"
    expect(dangerRepresentationForPath(path)).toEqual({
      branch: "master",
{'fraction_non_alphanumeric': 0.1474790412091156, 'fraction_numerical': 0.00035423308537017357, 'mean_word_length': 2.9068265682656826, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 33, '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': '3420189', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2530, 'n_words': 592}