codeflash-internal/js/cf-api/diff_utils.test.ts
Sarthak Agarwal 2f5a633b82 code-suggester package upgrade to npm registry (#1522)
### **PR Type**
- Enhancement



___

### **Description**
- Updated all import paths to use @codeflash-ai/code-suggester

- Upgraded code-suggester dependency version in package.json

- Applied type cast for octokit in getPullRequestHunks call


___



### **Changes walkthrough** 📝
<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>diff_utils.test.ts</strong><dd><code>Update
FileDiffContent import in tests</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/diff_utils.test.ts

- Changed FileDiffContent import path to @codeflash-ai/code-suggester


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-c0991313f3fcb63fa2a553856b85d7df03e6d99cf98d19277fe689025f89ede1">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>diff_utils.ts</strong><dd><code>Upgrade import paths
and apply octokit cast</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/diff_utils.ts

<li>Updated all import paths for code-suggester modules<br> <li> Cast
octokit argument in getPullRequestHunks call


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-f6212b48c365856b4ceb25da3ad03bd15fe171161cff046a31a92586272d4771">+4/-4</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>create-pr.ts</strong><dd><code>Fix FileDiffContent
import path in create-pr endpoint</code>&nbsp; &nbsp; &nbsp; &nbsp;
</dd></summary>
<hr>

js/cf-api/endpoints/create-pr.ts

- Updated FileDiffContent import to new package path


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-728a794bb81f944ae7db030f5e6ae1c1ba6888aa46e4be6846ebc5bd1ce12c2c">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>suggest-pr-changes.ts</strong><dd><code>Update
suggester and FileDiffContent import paths</code>&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/endpoints/suggest-pr-changes.ts

- Changed suggester and FileDiffContent imports to new package


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-b4a862986fd70827b8dabcb3157972bd0d5d507cf9c12e5fbf56f24979d073f3">+2/-2</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>

<summary><strong>create-pr-from-diffcontents.ts</strong><dd><code>Update
import path in create-pr-from-diffcontents</code>&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/github/create-pr-from-diffcontents.ts

- Updated FileDiffContent import to @codeflash-ai/code-suggester


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-b30d5e0c89beb70c0333a025726867aba5db7614911be3c46664bb59d1ab594a">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Dependencies</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>package.json</strong><dd><code>Upgrade code-suggester
dependency</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/package.json

<li>Upgraded dependency: replaced code-suggester with
<br>@codeflash-ai/code-suggester v5.0.2


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-53ddfb1f8a02f1231d3d15a2e694ffe1407d2cc01d3e685de5653b67fec571c7">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

___

> <details> <summary> Need help?</summary><li>Type <code>/help how to
...</code> in the comments thread for any questions about PR-Agent
usage.</li><li>Check out the <a
href="https://qodo-merge-docs.qodo.ai/usage-guide/">documentation</a>
for more information.</li></details>
2025-05-09 14:53:28 -04:00

108 lines
2.9 KiB
TypeScript

import { fileDiffsToMap, isDiffContentsWellFormed } from "./diff_utils.js"
import { type FileDiffContent } from "@codeflash-ai/code-suggester/build/src/types.js"
describe("fileDiffsToMap", () => {
it("should correctly convert an object to a Map", () => {
const obj: Record<string, FileDiffContent> = {
file1: {
oldContent: "old content",
newContent: "new content",
},
file2: {
oldContent: "old content 2",
newContent: "new content 2",
},
}
const map = fileDiffsToMap(obj)
expect(map.size).toBe(2)
expect(map.get("file1")).toEqual({
oldContent: "old content",
newContent: "new content",
})
expect(map.get("file2")).toEqual({
oldContent: "old content 2",
newContent: "new content 2",
})
})
it("should ignore non-object values", () => {
const obj: Record<string, any> = {
file1: "not an object",
file2: {
oldContent: "old content 2",
newContent: "new content 2",
},
}
const map = fileDiffsToMap(obj)
expect(map.size).toBe(1)
expect(map.get("file2")).toEqual({
oldContent: "old content 2",
newContent: "new content 2",
})
})
})
it("should return an empty Map when the input object is empty", () => {
const obj: Record<string, FileDiffContent> = {}
const map = fileDiffsToMap(obj)
expect(map.size).toBe(0)
})
it("should ignore nested objects", () => {
const obj: Record<string, any> = {
file1: {
oldContent: "old content",
newContent: "new content",
extra: { key: "value" },
},
}
const map = fileDiffsToMap(obj)
expect(map.size).toBe(1)
expect(map.get("file1")).toEqual({
oldContent: "old content",
newContent: "new content",
})
})
it("should ignore non-string values for oldContent and newContent", () => {
const obj: Record<string, any> = {
file1: {
oldContent: 123,
newContent: true,
},
}
const map = fileDiffsToMap(obj)
expect(map.size).toBe(0)
})
// Add tests for isDiffContentsWellFormed function
describe("isDiffContentsWellFormed", () => {
it("should return false for non-object input", () => {
const diffContents = "not an object"
expect(isDiffContentsWellFormed(diffContents)).toBe(false)
})
it("should return false for non-object values in the input object", () => {
const diffContents = { file1: "not an object" }
expect(isDiffContentsWellFormed(diffContents)).toBe(false)
})
it("should return false for objects that lack the oldContent or newContent properties", () => {
const diffContents = { file1: { notOldContent: "old content", notNewContent: "new content" } }
expect(isDiffContentsWellFormed(diffContents)).toBe(false)
})
it("should return false for oldContent or newContent properties that are not strings", () => {
const diffContents = { file1: { oldContent: 123, newContent: true } }
expect(isDiffContentsWellFormed(diffContents)).toBe(false)
})
})