|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { NotFoundError } from "@lithium-ai/core"; |
| 3 | +import { PostgresClusterAdapter } from "./cluster.port"; |
| 4 | +import { createClusterService } from "@lithium-ai/core"; |
| 5 | +import { setupTestDb } from "../test/setup"; |
| 6 | + |
| 7 | +describe("Cluster Integration", async () => { |
| 8 | + const clusterService = createClusterService( |
| 9 | + new PostgresClusterAdapter(await setupTestDb()) |
| 10 | + ); |
| 11 | + |
| 12 | + it("should create a root cluster with correct shape", async () => { |
| 13 | + // Arrange |
| 14 | + const name = crypto.randomUUID(); |
| 15 | + |
| 16 | + // Act |
| 17 | + const result = await clusterService.create({ name }); |
| 18 | + |
| 19 | + // Assert |
| 20 | + expect(result).toEqual({ |
| 21 | + success: true, |
| 22 | + value: { |
| 23 | + id: expect.any(String), |
| 24 | + parentId: null, |
| 25 | + path: name, |
| 26 | + name, |
| 27 | + description: null, |
| 28 | + createdAt: expect.any(Date), |
| 29 | + }, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should retrieve a created cluster by path", async () => { |
| 34 | + // Arrange |
| 35 | + const name = crypto.randomUUID(); |
| 36 | + const created = await clusterService.create({ name }); |
| 37 | + |
| 38 | + // Act |
| 39 | + const found = await clusterService.findByPath({ path: name }); |
| 40 | + |
| 41 | + // Assert |
| 42 | + expect(found).toEqual(created); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should build child path as parentPath.name", async () => { |
| 46 | + // Arrange |
| 47 | + const parentName = crypto.randomUUID(); |
| 48 | + const childName = crypto.randomUUID(); |
| 49 | + await clusterService.create({ name: parentName }); |
| 50 | + |
| 51 | + // Act |
| 52 | + const child = await clusterService.create({ |
| 53 | + name: childName, |
| 54 | + parentPath: parentName, |
| 55 | + }); |
| 56 | + |
| 57 | + // Assert |
| 58 | + expect(child).toEqual({ |
| 59 | + success: true, |
| 60 | + value: expect.objectContaining({ |
| 61 | + path: `${parentName}.${childName}`, |
| 62 | + name: childName, |
| 63 | + }), |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should set parentId to the parent cluster ID", async () => { |
| 68 | + // Arrange |
| 69 | + const parentName = crypto.randomUUID(); |
| 70 | + const parent = await clusterService.create({ name: parentName }); |
| 71 | + if (!parent.success) throw new Error("Parent creation failed"); |
| 72 | + |
| 73 | + // Act |
| 74 | + const child = await clusterService.create({ |
| 75 | + name: crypto.randomUUID(), |
| 76 | + parentPath: parentName, |
| 77 | + }); |
| 78 | + |
| 79 | + // Assert |
| 80 | + expect(child).toEqual({ |
| 81 | + success: true, |
| 82 | + value: expect.objectContaining({ |
| 83 | + parentId: parent.value.id, |
| 84 | + }), |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should include created cluster in list results with correct shape", async () => { |
| 89 | + // Arrange |
| 90 | + const name = crypto.randomUUID(); |
| 91 | + const created = await clusterService.create({ name }); |
| 92 | + if (!created.success) throw new Error("Create failed"); |
| 93 | + |
| 94 | + // Act |
| 95 | + const result = await clusterService.list(); |
| 96 | + if (!result.success) throw new Error("List failed"); |
| 97 | + |
| 98 | + // Assert |
| 99 | + expect(result.value).toContainEqual(created.value); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should return exact descendant IDs for parent and child", async () => { |
| 103 | + // Arrange |
| 104 | + const parentName = crypto.randomUUID(); |
| 105 | + const childName = crypto.randomUUID(); |
| 106 | + const parent = await clusterService.create({ name: parentName }); |
| 107 | + if (!parent.success) throw new Error("Parent creation failed"); |
| 108 | + const child = await clusterService.create({ |
| 109 | + name: childName, |
| 110 | + parentPath: parentName, |
| 111 | + }); |
| 112 | + if (!child.success) throw new Error("Child creation failed"); |
| 113 | + |
| 114 | + // Act |
| 115 | + const result = await clusterService.listDescendantIds({ path: parentName }); |
| 116 | + |
| 117 | + // Assert |
| 118 | + expect(result).toEqual({ |
| 119 | + success: true, |
| 120 | + value: expect.arrayContaining([parent.value.id, child.value.id]), |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it("should not include unrelated clusters in descendant IDs", async () => { |
| 125 | + // Arrange |
| 126 | + const parentName = crypto.randomUUID(); |
| 127 | + const unrelatedName = crypto.randomUUID(); |
| 128 | + const parent = await clusterService.create({ name: parentName }); |
| 129 | + if (!parent.success) throw new Error("Parent creation failed"); |
| 130 | + const unrelated = await clusterService.create({ name: unrelatedName }); |
| 131 | + if (!unrelated.success) throw new Error("Unrelated creation failed"); |
| 132 | + |
| 133 | + // Act |
| 134 | + const result = await clusterService.listDescendantIds({ path: parentName }); |
| 135 | + if (!result.success) throw new Error("listDescendantIds failed"); |
| 136 | + |
| 137 | + // Assert |
| 138 | + expect(result.value).toContain(parent.value.id); |
| 139 | + expect(result.value).not.toContain(unrelated.value.id); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should return NotFoundError for missing parent path", async () => { |
| 143 | + // Act |
| 144 | + const result = await clusterService.create({ |
| 145 | + name: crypto.randomUUID(), |
| 146 | + parentPath: "nonexistent", |
| 147 | + }); |
| 148 | + |
| 149 | + // Assert |
| 150 | + expect(result).toEqual({ |
| 151 | + success: false, |
| 152 | + error: expect.any(NotFoundError), |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments