n8n 連携: 期間の作業区間を集め、送信済み・重なりを判定する
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -91,5 +91,84 @@ class MakeBlocksTest(unittest.TestCase):
|
||||
self.assertEqual(blocks[0]["date"], "2026-01-05")
|
||||
|
||||
|
||||
def jsonl_line(obj):
|
||||
return json.dumps(obj, ensure_ascii=False) + "\n"
|
||||
|
||||
|
||||
def write_session(root, project_dir, sid, events, cwd):
|
||||
"""events: [(分, 'user'|'assistant'|'sidechain', 本文)] を jsonl にする。"""
|
||||
folder = Path(root) / project_dir
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
path = folder / f"{sid}.jsonl"
|
||||
with path.open("w", encoding="utf-8") as fh:
|
||||
for minute, kind, text in events:
|
||||
ts = chktime.iso(BASE + minute * MIN)
|
||||
if kind == "assistant":
|
||||
fh.write(jsonl_line({"type": "assistant", "timestamp": ts, "sessionId": sid, "cwd": cwd,
|
||||
"message": {"model": "m", "content": [{"type": "text", "text": text}]}}))
|
||||
else:
|
||||
fh.write(jsonl_line({"type": "user", "timestamp": ts, "sessionId": sid, "cwd": cwd,
|
||||
"isSidechain": kind == "sidechain", "message": {"role": "user", "content": text}}))
|
||||
end = BASE + max(m for m, _, _ in events) * MIN
|
||||
import os
|
||||
os.utime(path, (end, end))
|
||||
return path
|
||||
|
||||
|
||||
class CollectAndMarkTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.root = Path(self.tmp.name)
|
||||
self.store = chktime.Store(self.root)
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def test_collect_blocks_names_and_range(self):
|
||||
write_session(self.root, "C--debug-demo", "s1",
|
||||
[(0, "user", "設計して"), (10, "assistant", "ok"), (60, "user", "続き"), (70, "assistant", "ok")],
|
||||
r"C:\debug\demo")
|
||||
blocks = chktime.collect_blocks(self.store, "", date(2026, 1, 5), date(2026, 1, 5), 30, 5, now=LATER)
|
||||
self.assertEqual(len(blocks), 2)
|
||||
self.assertEqual(blocks[0]["taskName"], "[demo] 設計して")
|
||||
self.assertEqual(blocks[0]["project"], "C--debug-demo")
|
||||
none = chktime.collect_blocks(self.store, "", date(2026, 1, 6), date(2026, 1, 6), 30, 5, now=LATER)
|
||||
self.assertEqual(none, [])
|
||||
|
||||
def test_sidechain_does_not_start_turn(self):
|
||||
write_session(self.root, "C--debug-demo", "s2",
|
||||
[(0, "user", "調べて"), (10, "assistant", "ok"), (45, "sidechain", "sub"), (80, "user", "次"), (90, "assistant", "ok")],
|
||||
r"C:\debug\demo")
|
||||
blocks = chktime.collect_blocks(self.store, "", date(2026, 1, 5), date(2026, 1, 5), 30, 5, now=LATER)
|
||||
# サブエージェントの行は直前のターンを 45 分まで伸ばすだけ。80 分の発言はその 35 分後なので別の区間。
|
||||
self.assertEqual([b["minutes"] for b in blocks], [45, 10])
|
||||
|
||||
def test_midnight_block_belongs_to_start_date(self):
|
||||
start = datetime(2026, 1, 5, 23, 50).timestamp()
|
||||
minutes = (start - BASE) / MIN
|
||||
write_session(self.root, "C--debug-demo", "s3",
|
||||
[(minutes, "user", "夜"), (minutes + 20, "assistant", "ok")], r"C:\debug\demo")
|
||||
day1 = chktime.collect_blocks(self.store, "", date(2026, 1, 5), date(2026, 1, 5), 30, 5, now=LATER + 86400)
|
||||
day2 = chktime.collect_blocks(self.store, "", date(2026, 1, 6), date(2026, 1, 6), 30, 5, now=LATER + 86400)
|
||||
self.assertEqual([b["date"] for b in day1], ["2026-01-05"])
|
||||
self.assertEqual(day2, [])
|
||||
|
||||
def test_mark_blocks(self):
|
||||
b1 = {"source": "chktime:a:1", "startEpoch": BASE, "endEpoch": BASE + 600, "start": "", "end": "", "taskName": "A"}
|
||||
b2 = {"source": "chktime:b:2", "startEpoch": BASE + 600, "endEpoch": BASE + 1200, "start": "", "end": "", "taskName": "B"}
|
||||
b3 = {"source": "chktime:c:3", "startEpoch": BASE + 1199, "endEpoch": BASE + 1800, "start": "", "end": "", "taskName": "C"}
|
||||
rows = [
|
||||
{"id": 1, "source": "chktime:a:1", "start_time": chktime.iso(BASE), "end_time": chktime.iso(BASE + 600), "task": "A"},
|
||||
{"id": 2, "source": "", "start_time": chktime.iso(BASE + 1700), "end_time": None, "task": "手で記録"},
|
||||
]
|
||||
chktime.mark_blocks([b1, b2, b3], rows, ["chktime:b:2"], now=BASE + 3600)
|
||||
self.assertTrue(b1["sent"])
|
||||
self.assertEqual(b1["overlaps"], []) # 自分の行は除く。b2 とは接しているだけ
|
||||
self.assertFalse(b2["sent"])
|
||||
self.assertTrue(b2["deleted"])
|
||||
self.assertEqual([o["source"] for o in b2["overlaps"]], ["chktime:c:3"]) # 1 秒重なる
|
||||
self.assertEqual(sorted(o["kind"] for o in b3["overlaps"]), ["block", "row"]) # 進行中の行 (終了 = 今) とも重なる
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user