|
118 | 118 | expect(response).to have_http_status(:unprocessable_content) |
119 | 119 | end |
120 | 120 |
|
| 121 | + describe "filing on create" do |
| 122 | + it "files the plan via folder_path, creating the hierarchy in the caller's library" do |
| 123 | + post api_v1_plans_path, params: { title: "Filed Plan", content: "# Filed", folder_path: "Team EBT/Q3" }, headers: headers, as: :json |
| 124 | + expect(response).to have_http_status(:created) |
| 125 | + body = JSON.parse(response.body) |
| 126 | + expect(body["folder_path"]).to eq("Team EBT/Q3") |
| 127 | + |
| 128 | + placement = alice.library.placements.find_by(plan_id: body.fetch("id")) |
| 129 | + expect(placement.folder.path).to eq("Team EBT/Q3") |
| 130 | + expect(alice.library.folders.count).to eq(2) |
| 131 | + end |
| 132 | + |
| 133 | + it "files the plan via folder_id" do |
| 134 | + folder = create(:folder, name: "Infra", created_by_user: alice) |
| 135 | + post api_v1_plans_path, params: { title: "Filed Plan", content: "# Filed", folder_id: folder.id }, headers: headers, as: :json |
| 136 | + expect(response).to have_http_status(:created) |
| 137 | + expect(JSON.parse(response.body)["folder_id"]).to eq(folder.id) |
| 138 | + end |
| 139 | + |
| 140 | + it "records the filing in the library audit log with agent attribution" do |
| 141 | + post api_v1_plans_path, params: { title: "Filed Plan", content: "# Filed", folder_path: "Infra", agent_name: "Claude" }, headers: headers, as: :json |
| 142 | + expect(response).to have_http_status(:created) |
| 143 | + |
| 144 | + event = alice.library.library_events.find_by(event_type: "plan_filed") |
| 145 | + expect(event).to be_present |
| 146 | + expect(event.actor_type).to eq("local_agent") |
| 147 | + expect(event.agent_name).to eq("Claude") |
| 148 | + end |
| 149 | + |
| 150 | + it "rolls back the whole create when the folder_id is unknown" do |
| 151 | + expect { |
| 152 | + post api_v1_plans_path, params: { title: "Doomed Plan", content: "# Doomed", folder_id: "nope" }, headers: headers, as: :json |
| 153 | + }.not_to change(CoPlan::Plan, :count) |
| 154 | + expect(response).to have_http_status(:unprocessable_content) |
| 155 | + expect(JSON.parse(response.body)["error"]).to include("Unknown folder_id") |
| 156 | + end |
| 157 | + |
| 158 | + it "does not emit a plan_created analytics event for a rolled-back create" do |
| 159 | + events = capture_analytics_events do |
| 160 | + post api_v1_plans_path, params: { title: "Doomed Plan", content: "# Doomed", folder_id: "nope" }, headers: headers, as: :json |
| 161 | + end |
| 162 | + expect(response).to have_http_status(:unprocessable_content) |
| 163 | + expect(events.select { |name, _| name == "plan_created" }).to be_empty |
| 164 | + end |
| 165 | + |
| 166 | + it "emits plan_created exactly once for a successful filed create" do |
| 167 | + events = capture_analytics_events do |
| 168 | + post api_v1_plans_path, params: { title: "Filed Plan", content: "# Filed", folder_path: "Infra" }, headers: headers, as: :json |
| 169 | + end |
| 170 | + expect(response).to have_http_status(:created) |
| 171 | + expect(events.select { |name, _| name == "plan_created" }.length).to eq(1) |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + describe "tags on create" do |
| 176 | + it "applies the plan type's default_tags" do |
| 177 | + create(:plan_type, name: "design-doc", default_tags: ["design", "architecture"]) |
| 178 | + post api_v1_plans_path, params: { title: "Tagged Plan", content: "# Tagged", plan_type: "design-doc" }, headers: headers, as: :json |
| 179 | + expect(response).to have_http_status(:created) |
| 180 | + expect(JSON.parse(response.body)["tags"]).to match_array(["design", "architecture"]) |
| 181 | + end |
| 182 | + |
| 183 | + it "merges explicit tags with the type's default_tags" do |
| 184 | + create(:plan_type, name: "design-doc", default_tags: ["design"]) |
| 185 | + post api_v1_plans_path, params: { title: "Tagged Plan", content: "# Tagged", plan_type: "design-doc", tags: ["pricing", "design"] }, headers: headers, as: :json |
| 186 | + expect(response).to have_http_status(:created) |
| 187 | + expect(JSON.parse(response.body)["tags"]).to match_array(["design", "pricing"]) |
| 188 | + end |
| 189 | + |
| 190 | + it "accepts explicit tags without a plan_type" do |
| 191 | + post api_v1_plans_path, params: { title: "Tagged Plan", content: "# Tagged", tags: ["pricing"] }, headers: headers, as: :json |
| 192 | + expect(response).to have_http_status(:created) |
| 193 | + expect(JSON.parse(response.body)["tags"]).to eq(["pricing"]) |
| 194 | + end |
| 195 | + end |
| 196 | + |
121 | 197 | describe "PATCH /api/v1/plans/:id" do |
122 | 198 | it "updates plan title" do |
123 | 199 | patch api_v1_plan_path(plan), params: { title: "New Title" }, headers: headers, as: :json |
|
0 commit comments