| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- import { describe, expect, it } from "vitest";
- import { buildTreemappingActivityModel } from "../src/lib/treemappingActivity.js";
- import type { TaskChatMessage, TaskChatToolCall } from "../src/lib/taskChatMessageTypes.js";
- function message(toolCalls: TaskChatToolCall[]): TaskChatMessage {
- return {
- id: "assistant-1",
- role: "assistant",
- content: "",
- timestamp: 1,
- toolCalls,
- };
- }
- function toolCall(overrides: Partial<TaskChatToolCall>): TaskChatToolCall {
- return {
- toolId: overrides.toolId ?? "tool-1",
- kind: overrides.kind ?? "read",
- title: overrides.title,
- input: overrides.input ?? {},
- output: overrides.output,
- raw: overrides.raw,
- status: overrides.status ?? "completed",
- startedAt: overrides.startedAt ?? 10,
- };
- }
- describe("buildTreemappingActivityModel", () => {
- it("derives read, search, edit, write and delete file activity", () => {
- const model = buildTreemappingActivityModel(
- message([
- toolCall({
- toolId: "read-1",
- kind: "read",
- input: { file_path: "/workspace/src/app.ts" },
- }),
- toolCall({
- toolId: "search-1",
- kind: "search",
- output: { files: ["src/app.ts", "src/util.ts"] },
- }),
- toolCall({
- toolId: "edit-1",
- kind: "edit",
- raw: {
- changes: {
- "src/app.ts": {
- type: "update",
- oldText: "one\n",
- newText: "one\ntwo\n",
- },
- },
- },
- }),
- toolCall({
- toolId: "write-1",
- kind: "write",
- input: { file_path: "src/new.ts", content: "hello\nworld\n" },
- }),
- toolCall({
- toolId: "delete-1",
- kind: "delete",
- input: { file_path: "src/old.ts", content: "bye\n" },
- }),
- ]),
- "/workspace",
- );
- const app = model.files.find((file) => file.path === "src/app.ts");
- const util = model.files.find((file) => file.path === "src/util.ts");
- const written = model.files.find((file) => file.path === "src/new.ts");
- const deleted = model.files.find((file) => file.path === "src/old.ts");
- expect(app).toMatchObject({ kind: "modified", views: 2, added: 1, removed: 0 });
- expect(util).toMatchObject({ kind: "viewed", views: 1 });
- expect(written).toMatchObject({ kind: "written", added: 2 });
- expect(deleted).toMatchObject({ kind: "deleted", removed: 1 });
- });
- it("keeps write priority when a file is written then modified", () => {
- const model = buildTreemappingActivityModel(
- message([
- toolCall({
- toolId: "write-1",
- kind: "write",
- input: { file_path: "src/new.ts", content: "hello\n" },
- }),
- toolCall({
- toolId: "edit-1",
- kind: "edit",
- raw: {
- changes: {
- "src/new.ts": {
- type: "update",
- oldText: "hello\n",
- newText: "hello\nworld\n",
- },
- },
- },
- }),
- ]),
- "/workspace",
- );
- expect(model.files[0]).toMatchObject({
- path: "src/new.ts",
- kind: "written",
- added: 2,
- removed: 0,
- });
- });
- it("keeps delete priority over write, edit and view for the same file", () => {
- const model = buildTreemappingActivityModel(
- message([
- toolCall({
- toolId: "read-1",
- kind: "read",
- input: { file_path: "src/new.ts" },
- }),
- toolCall({
- toolId: "write-1",
- kind: "write",
- input: { file_path: "src/new.ts", content: "hello\n" },
- }),
- toolCall({
- toolId: "edit-1",
- kind: "edit",
- raw: {
- changes: {
- "src/new.ts": {
- type: "update",
- oldText: "hello\n",
- newText: "hello\nworld\n",
- },
- },
- },
- }),
- toolCall({
- toolId: "delete-1",
- kind: "delete",
- input: { file_path: "src/new.ts", content: "hello\nworld\n" },
- }),
- ]),
- "/workspace",
- );
- expect(model.files[0]).toMatchObject({
- path: "src/new.ts",
- kind: "deleted",
- views: 1,
- added: 2,
- removed: 1,
- });
- });
- it("tracks directory views from ls and search scopes", () => {
- const model = buildTreemappingActivityModel(
- message([
- toolCall({
- toolId: "ls-1",
- kind: "list",
- input: { path: "src/components" },
- }),
- toolCall({
- toolId: "grep-1",
- kind: "grep",
- input: { directory: "src" },
- }),
- ]),
- "/workspace",
- );
- expect(model.directories).toEqual(
- expect.arrayContaining([
- expect.objectContaining({ path: "src/components", views: 1 }),
- expect.objectContaining({ path: "src", views: 1 }),
- ]),
- );
- });
- it("only consumes structured changes for shell calls", () => {
- const model = buildTreemappingActivityModel(
- message([
- toolCall({
- toolId: "shell-1",
- kind: "execute",
- input: { command: "cat src/ignored.ts && rm src/also-ignored.ts" },
- output: "deleted src/also-ignored.ts\nread src/ignored.ts",
- }),
- toolCall({
- toolId: "shell-2",
- kind: "execute",
- raw: {
- rawOutput: {
- changes: {
- "src/changed.ts": { type: "update", added: 3, removed: 1 },
- },
- },
- },
- }),
- ]),
- "/workspace",
- );
- expect(model.files.map((file) => file.path)).toEqual(["src/changed.ts"]);
- expect(model.files[0]).toMatchObject({ kind: "modified", added: 3, removed: 1 });
- });
- it("uses ZCode file_diff display stats without treating title=Write as written", () => {
- const filePath = "/workspace/src/app/components/TodoList.tsx";
- const model = buildTreemappingActivityModel(
- message([
- toolCall({
- toolId: "zcode-edit-1",
- kind: "edit",
- title: "Write",
- input: { file_path: filePath, content: "large content" },
- output: {
- success: true,
- display: {
- kind: "file_diff",
- filePath,
- additions: 541,
- deletions: 110,
- structuredPatch: [
- {
- oldStart: 1,
- oldLines: 44,
- newStart: 1,
- newLines: 502,
- lines: [
- ' "use client";',
- "-import { useState } from \"react\";",
- "+import { useState, useEffect } from \"react\";",
- ],
- },
- ],
- },
- },
- raw: {
- rawOutput: {
- success: true,
- display: {
- kind: "file_diff",
- filePath,
- additions: 541,
- deletions: 110,
- structuredPatch: [
- {
- oldStart: 1,
- oldLines: 44,
- newStart: 1,
- newLines: 502,
- lines: [
- ' "use client";',
- "-import { useState } from \"react\";",
- "+import { useState, useEffect } from \"react\";",
- ],
- },
- ],
- },
- },
- },
- }),
- ]),
- "/workspace",
- );
- expect(model.files).toHaveLength(1);
- expect(model.files[0]).toMatchObject({
- path: "src/app/components/TodoList.tsx",
- kind: "modified",
- added: 541,
- removed: 110,
- });
- });
- it("uses rawOutput content type as the ZCode write/edit/delete source of truth", () => {
- const createPath = "/workspace/src/created.ts";
- const updatePath = "/workspace/src/updated.ts";
- const deletePath = "/workspace/src/deleted.ts";
- const model = buildTreemappingActivityModel(
- message([
- toolCall({
- toolId: "zcode-create-1",
- kind: "edit",
- title: "Write",
- input: { file_path: createPath, content: "one\ntwo\n" },
- raw: {
- rawOutput: {
- content: JSON.stringify({
- type: "create",
- filePath: createPath,
- content: "one\ntwo\n",
- }),
- display: {
- kind: "file_diff",
- filePath: createPath,
- additions: 2,
- deletions: 0,
- },
- },
- },
- }),
- toolCall({
- toolId: "zcode-update-1",
- kind: "edit",
- title: "Write",
- input: { file_path: updatePath, content: "changed\n" },
- raw: {
- rawOutput: {
- content: JSON.stringify({
- type: "update",
- filePath: updatePath,
- content: "changed\n",
- }),
- display: {
- kind: "file_diff",
- filePath: updatePath,
- additions: 3,
- deletions: 1,
- },
- },
- },
- }),
- toolCall({
- toolId: "zcode-delete-1",
- kind: "edit",
- title: "Write",
- input: { file_path: deletePath },
- raw: {
- rawOutput: {
- content: JSON.stringify({
- type: "remove",
- filePath: deletePath,
- }),
- display: {
- kind: "file_diff",
- filePath: deletePath,
- additions: 0,
- deletions: 4,
- },
- },
- },
- }),
- ]),
- "/workspace",
- );
- expect(model.files).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- path: "src/created.ts",
- kind: "written",
- added: 2,
- removed: 0,
- }),
- expect.objectContaining({
- path: "src/updated.ts",
- kind: "modified",
- added: 3,
- removed: 1,
- }),
- expect.objectContaining({
- path: "src/deleted.ts",
- kind: "deleted",
- added: 0,
- removed: 4,
- }),
- ]),
- );
- });
- });
|