feat(task): add real-time single-task todo sync via OpenCode API

- Add syncTaskTodoUpdate function for immediate todo updates
- Integrate with TaskCreate and TaskUpdate tools
- Preserve existing todos when updating single task
- Add comprehensive tests for new sync function

🤖 Generated with [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
YeonGyu-Kim
2026-02-02 15:05:07 +09:00
parent 418cf35886
commit 0ea92124a7
4 changed files with 415 additions and 200 deletions
+222 -94
View File
@@ -1,6 +1,39 @@
import { describe, it, expect, beforeEach, vi } from "bun:test"
import type { Task } from "../../features/claude-tasks/types"
import { syncTaskToTodo, syncAllTasksToTodos, type TodoInfo } from "./todo-sync"
/// <reference path="../../types/bun-test.d.ts" />
type TestBody = () => unknown | Promise<unknown>;
type TestDecl = (name: string, fn?: TestBody) => void;
type Hook = (fn: TestBody) => void;
type Expectation = {
toBe: (...args: unknown[]) => unknown;
toBeNull: (...args: unknown[]) => unknown;
toEqual: (...args: unknown[]) => unknown;
toHaveProperty: (...args: unknown[]) => unknown;
toMatch: (...args: unknown[]) => unknown;
toBeDefined: (...args: unknown[]) => unknown;
toContain: (...args: unknown[]) => unknown;
toBeUndefined: (...args: unknown[]) => unknown;
toHaveBeenCalled: (...args: unknown[]) => unknown;
toHaveBeenCalledTimes: (...args: unknown[]) => unknown;
toHaveBeenCalledWith: (...args: unknown[]) => unknown;
not: Expectation;
};
type Expect = (value?: unknown) => Expectation;
declare const describe: TestDecl;
declare const it: TestDecl;
declare const test: TestDecl;
declare const expect: Expect;
declare const beforeEach: Hook;
declare const afterEach: Hook;
declare const beforeAll: Hook;
declare const afterAll: Hook;
declare const vi: { fn: (...args: unknown[]) => unknown };
import type { Task } from "../../features/claude-tasks/types";
import {
syncTaskToTodo,
syncAllTasksToTodos,
syncTaskTodoUpdate,
type TodoInfo,
} from "./todo-sync";
describe("syncTaskToTodo", () => {
it("converts pending task to pending todo", () => {
@@ -12,10 +45,10 @@ describe("syncTaskToTodo", () => {
status: "pending",
blocks: [],
blockedBy: [],
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result).toEqual({
@@ -23,8 +56,8 @@ describe("syncTaskToTodo", () => {
content: "Fix bug",
status: "pending",
priority: undefined,
})
})
});
});
it("converts in_progress task to in_progress todo", () => {
// given
@@ -35,15 +68,15 @@ describe("syncTaskToTodo", () => {
status: "in_progress",
blocks: [],
blockedBy: [],
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result?.status).toBe("in_progress")
expect(result?.content).toBe("Implement feature")
})
expect(result?.status).toBe("in_progress");
expect(result?.content).toBe("Implement feature");
});
it("converts completed task to completed todo", () => {
// given
@@ -54,14 +87,14 @@ describe("syncTaskToTodo", () => {
status: "completed",
blocks: [],
blockedBy: [],
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result?.status).toBe("completed")
})
expect(result?.status).toBe("completed");
});
it("returns null for deleted task", () => {
// given
@@ -72,14 +105,14 @@ describe("syncTaskToTodo", () => {
status: "deleted",
blocks: [],
blockedBy: [],
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result).toBeNull()
})
expect(result).toBeNull();
});
it("extracts priority from metadata", () => {
// given
@@ -91,14 +124,14 @@ describe("syncTaskToTodo", () => {
blocks: [],
blockedBy: [],
metadata: { priority: "high" },
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result?.priority).toBe("high")
})
expect(result?.priority).toBe("high");
});
it("handles medium priority", () => {
// given
@@ -110,14 +143,14 @@ describe("syncTaskToTodo", () => {
blocks: [],
blockedBy: [],
metadata: { priority: "medium" },
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result?.priority).toBe("medium")
})
expect(result?.priority).toBe("medium");
});
it("handles low priority", () => {
// given
@@ -129,14 +162,14 @@ describe("syncTaskToTodo", () => {
blocks: [],
blockedBy: [],
metadata: { priority: "low" },
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result?.priority).toBe("low")
})
expect(result?.priority).toBe("low");
});
it("ignores invalid priority values", () => {
// given
@@ -148,14 +181,14 @@ describe("syncTaskToTodo", () => {
blocks: [],
blockedBy: [],
metadata: { priority: "urgent" },
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result?.priority).toBeUndefined()
})
expect(result?.priority).toBeUndefined();
});
it("handles missing metadata", () => {
// given
@@ -166,14 +199,14 @@ describe("syncTaskToTodo", () => {
status: "pending",
blocks: [],
blockedBy: [],
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result?.priority).toBeUndefined()
})
expect(result?.priority).toBeUndefined();
});
it("uses subject as todo content", () => {
// given
@@ -184,18 +217,18 @@ describe("syncTaskToTodo", () => {
status: "pending",
blocks: [],
blockedBy: [],
}
};
// when
const result = syncTaskToTodo(task)
const result = syncTaskToTodo(task);
// then
expect(result?.content).toBe("This is the subject")
})
})
expect(result?.content).toBe("This is the subject");
});
});
describe("syncAllTasksToTodos", () => {
let mockCtx: any
describe("syncTaskTodoUpdate", () => {
let mockCtx: any;
beforeEach(() => {
mockCtx = {
@@ -204,8 +237,103 @@ describe("syncAllTasksToTodos", () => {
todo: vi.fn(),
},
},
}
})
};
});
it("writes updated todo and preserves existing items", async () => {
// given
const task: Task = {
id: "T-1",
subject: "Updated task",
description: "",
status: "in_progress",
blocks: [],
blockedBy: [],
};
const currentTodos: TodoInfo[] = [
{ id: "T-1", content: "Old task", status: "pending" },
{ id: "T-2", content: "Keep task", status: "pending" },
];
mockCtx.client.session.todo.mockResolvedValue({ data: currentTodos });
const payload: { sessionID: string; todos: TodoInfo[] } = {
sessionID: "",
todos: [],
};
let calls = 0;
const writer = async (input: { sessionID: string; todos: TodoInfo[] }) => {
calls += 1;
payload.sessionID = input.sessionID;
payload.todos = input.todos;
};
// when
await syncTaskTodoUpdate(mockCtx, task, "session-1", writer);
// then
expect(calls).toBe(1);
expect(payload.sessionID).toBe("session-1");
expect(payload.todos.length).toBe(2);
expect(
payload.todos.find((todo: TodoInfo) => todo.id === "T-1")?.content,
).toBe("Updated task");
expect(payload.todos.some((todo: TodoInfo) => todo.id === "T-2")).toBe(
true,
);
});
it("removes deleted task from todos", async () => {
// given
const task: Task = {
id: "T-1",
subject: "Deleted task",
description: "",
status: "deleted",
blocks: [],
blockedBy: [],
};
const currentTodos: TodoInfo[] = [
{ id: "T-1", content: "Old task", status: "pending" },
{ id: "T-2", content: "Keep task", status: "pending" },
];
mockCtx.client.session.todo.mockResolvedValue(currentTodos);
const payload: { sessionID: string; todos: TodoInfo[] } = {
sessionID: "",
todos: [],
};
let calls = 0;
const writer = async (input: { sessionID: string; todos: TodoInfo[] }) => {
calls += 1;
payload.sessionID = input.sessionID;
payload.todos = input.todos;
};
// when
await syncTaskTodoUpdate(mockCtx, task, "session-1", writer);
// then
expect(calls).toBe(1);
expect(payload.todos.length).toBe(1);
expect(payload.todos.some((todo: TodoInfo) => todo.id === "T-1")).toBe(
false,
);
expect(payload.todos.some((todo: TodoInfo) => todo.id === "T-2")).toBe(
true,
);
});
});
describe("syncAllTasksToTodos", () => {
let mockCtx: any;
beforeEach(() => {
mockCtx = {
client: {
session: {
todo: vi.fn(),
},
},
};
});
it("fetches current todos from OpenCode", async () => {
// given
@@ -218,45 +346,45 @@ describe("syncAllTasksToTodos", () => {
blocks: [],
blockedBy: [],
},
]
];
const currentTodos: TodoInfo[] = [
{
id: "T-existing",
content: "Existing todo",
status: "pending",
},
]
mockCtx.client.session.todo.mockResolvedValue(currentTodos)
];
mockCtx.client.session.todo.mockResolvedValue(currentTodos);
// when
await syncAllTasksToTodos(mockCtx, tasks, "session-1")
await syncAllTasksToTodos(mockCtx, tasks, "session-1");
// then
expect(mockCtx.client.session.todo).toHaveBeenCalledWith({
path: { id: "session-1" },
})
})
});
});
it("handles API response with data property", async () => {
// given
const tasks: Task[] = []
const tasks: Task[] = [];
const currentTodos: TodoInfo[] = [
{
id: "T-1",
content: "Todo 1",
status: "pending",
},
]
];
mockCtx.client.session.todo.mockResolvedValue({
data: currentTodos,
})
});
// when
await syncAllTasksToTodos(mockCtx, tasks, "session-1")
await syncAllTasksToTodos(mockCtx, tasks, "session-1");
// then
expect(mockCtx.client.session.todo).toHaveBeenCalled()
})
expect(mockCtx.client.session.todo).toHaveBeenCalled();
});
it("gracefully handles fetch failure", async () => {
// given
@@ -269,15 +397,15 @@ describe("syncAllTasksToTodos", () => {
blocks: [],
blockedBy: [],
},
]
mockCtx.client.session.todo.mockRejectedValue(new Error("API error"))
];
mockCtx.client.session.todo.mockRejectedValue(new Error("API error"));
// when
const result = await syncAllTasksToTodos(mockCtx, tasks, "session-1")
const result = await syncAllTasksToTodos(mockCtx, tasks, "session-1");
// then
expect(result).toBeUndefined()
})
expect(result).toBeUndefined();
});
it("converts multiple tasks to todos", async () => {
// given
@@ -300,15 +428,15 @@ describe("syncAllTasksToTodos", () => {
blockedBy: [],
metadata: { priority: "low" },
},
]
mockCtx.client.session.todo.mockResolvedValue([])
];
mockCtx.client.session.todo.mockResolvedValue([]);
// when
await syncAllTasksToTodos(mockCtx, tasks, "session-1")
await syncAllTasksToTodos(mockCtx, tasks, "session-1");
// then
expect(mockCtx.client.session.todo).toHaveBeenCalled()
})
expect(mockCtx.client.session.todo).toHaveBeenCalled();
});
it("removes deleted tasks from todo list", async () => {
// given
@@ -321,22 +449,22 @@ describe("syncAllTasksToTodos", () => {
blocks: [],
blockedBy: [],
},
]
];
const currentTodos: TodoInfo[] = [
{
id: "T-1",
content: "Task 1",
status: "pending",
},
]
mockCtx.client.session.todo.mockResolvedValue(currentTodos)
];
mockCtx.client.session.todo.mockResolvedValue(currentTodos);
// when
await syncAllTasksToTodos(mockCtx, tasks, "session-1")
await syncAllTasksToTodos(mockCtx, tasks, "session-1");
// then
expect(mockCtx.client.session.todo).toHaveBeenCalled()
})
expect(mockCtx.client.session.todo).toHaveBeenCalled();
});
it("preserves existing todos not in task list", async () => {
// given
@@ -349,7 +477,7 @@ describe("syncAllTasksToTodos", () => {
blocks: [],
blockedBy: [],
},
]
];
const currentTodos: TodoInfo[] = [
{
id: "T-1",
@@ -361,27 +489,27 @@ describe("syncAllTasksToTodos", () => {
content: "Existing todo",
status: "pending",
},
]
mockCtx.client.session.todo.mockResolvedValue(currentTodos)
];
mockCtx.client.session.todo.mockResolvedValue(currentTodos);
// when
await syncAllTasksToTodos(mockCtx, tasks, "session-1")
await syncAllTasksToTodos(mockCtx, tasks, "session-1");
// then
expect(mockCtx.client.session.todo).toHaveBeenCalled()
})
expect(mockCtx.client.session.todo).toHaveBeenCalled();
});
it("handles empty task list", async () => {
// given
const tasks: Task[] = []
mockCtx.client.session.todo.mockResolvedValue([])
const tasks: Task[] = [];
mockCtx.client.session.todo.mockResolvedValue([]);
// when
await syncAllTasksToTodos(mockCtx, tasks, "session-1")
await syncAllTasksToTodos(mockCtx, tasks, "session-1");
// then
expect(mockCtx.client.session.todo).toHaveBeenCalled()
})
expect(mockCtx.client.session.todo).toHaveBeenCalled();
});
it("handles undefined sessionID", async () => {
// given
@@ -394,15 +522,15 @@ describe("syncAllTasksToTodos", () => {
blocks: [],
blockedBy: [],
},
]
mockCtx.client.session.todo.mockResolvedValue([])
];
mockCtx.client.session.todo.mockResolvedValue([]);
// when
await syncAllTasksToTodos(mockCtx, tasks)
await syncAllTasksToTodos(mockCtx, tasks);
// then
expect(mockCtx.client.session.todo).toHaveBeenCalledWith({
path: { id: "" },
})
})
})
});
});
});