From c83ef32fe1ac5064f6e94070282a769e70acb5ed Mon Sep 17 00:00:00 2001
From: "google-labs-jules[bot]"
<161369871+google-labs-jules[bot]@users.noreply.github.com>
Date: Sun, 19 Jul 2026 14:18:01 +0000
Subject: [PATCH] test: implement unit tests for updateFileContentInTree
Adds comprehensive unit tests for `updateFileContentInTree` using Node's native test runner (`node:test`) and tsx to run TypeScript tests.
Includes tests for:
- Root-level file updates
- Deeply nested file updates
- Non-existent file paths
- Empty arrays
- Undefined folder children list
Co-authored-by: beingniloy <235952944+beingniloy@users.noreply.github.com>
---
src/utils.test.ts | 112 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 112 insertions(+)
create mode 100644 src/utils.test.ts
diff --git a/src/utils.test.ts b/src/utils.test.ts
new file mode 100644
index 0000000..8e33fb6
--- /dev/null
+++ b/src/utils.test.ts
@@ -0,0 +1,112 @@
+import { describe, it } from 'node:test';
+import assert from 'node:assert/strict';
+import { updateFileContentInTree } from './utils';
+import { FileSystemItem } from './types';
+
+describe('updateFileContentInTree', () => {
+ it('should update content and modified state of a file at the root level', () => {
+ const items: FileSystemItem[] = [
+ {
+ name: 'index.js',
+ path: 'index.js',
+ isFolder: false,
+ content: 'console.log("hello");',
+ modified: false,
+ },
+ {
+ name: 'styles.css',
+ path: 'styles.css',
+ isFolder: false,
+ content: 'body { color: red; }',
+ modified: false,
+ },
+ ];
+
+ const result = updateFileContentInTree(items, 'index.js', 'console.log("world");', true);
+
+ // Verify index.js is updated
+ assert.equal(result[0].content, 'console.log("world");');
+ assert.equal(result[0].modified, true);
+
+ // Verify other elements are not modified
+ assert.deepEqual(result[1], items[1]);
+ });
+
+ it('should update content and modified state of a nested file deep within a folder hierarchy', () => {
+ const items: FileSystemItem[] = [
+ {
+ name: 'src',
+ path: 'src',
+ isFolder: true,
+ children: [
+ {
+ name: 'components',
+ path: 'src/components',
+ isFolder: true,
+ children: [
+ {
+ name: 'Button.tsx',
+ path: 'src/components/Button.tsx',
+ isFolder: false,
+ content: 'export const Button = () => null;',
+ modified: false,
+ }
+ ]
+ },
+ {
+ name: 'index.tsx',
+ path: 'src/index.tsx',
+ isFolder: false,
+ content: 'import "./index.css";',
+ modified: false,
+ }
+ ]
+ }
+ ];
+
+ const result = updateFileContentInTree(items, 'src/components/Button.tsx', 'export const Button = () => ;', true);
+
+ const buttonFile = result[0].children?.[0].children?.[0];
+ assert.ok(buttonFile);
+ assert.equal(buttonFile.content, 'export const Button = () => ;');
+ assert.equal(buttonFile.modified, true);
+
+ // Verify the rest of the tree is untouched
+ const indexFile = result[0].children?.[1];
+ assert.deepEqual(indexFile, items[0].children?.[1]);
+ });
+
+ it('should return unchanged tree if target path does not exist', () => {
+ const items: FileSystemItem[] = [
+ {
+ name: 'index.js',
+ path: 'index.js',
+ isFolder: false,
+ content: 'console.log("hello");',
+ modified: false,
+ }
+ ];
+
+ const result = updateFileContentInTree(items, 'non-existent.js', 'new content', true);
+ assert.deepEqual(result, items);
+ });
+
+ it('should handle an empty items array', () => {
+ const result = updateFileContentInTree([], 'index.js', 'new content', true);
+ assert.deepEqual(result, []);
+ });
+
+ it('should handle tree items with undefined children gracefully', () => {
+ const items: FileSystemItem[] = [
+ {
+ name: 'folder',
+ path: 'folder',
+ isFolder: true,
+ children: undefined
+ }
+ ];
+
+ const result = updateFileContentInTree(items, 'folder/file.js', 'content', true);
+ assert.deepEqual(result, items);
+ });
+});