|
1 | 1 | package prd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
4 | 6 | "os" |
5 | 7 | "path/filepath" |
6 | 8 | "testing" |
@@ -303,3 +305,141 @@ func TestPRD_Save_PreservesInProgress(t *testing.T) { |
303 | 305 | t.Error("expected InProgress to be preserved as true") |
304 | 306 | } |
305 | 307 | } |
| 308 | + |
| 309 | +func TestPRD_NextStoryContext_ReturnsHighestPriority(t *testing.T) { |
| 310 | + p := &PRD{ |
| 311 | + Project: "Test", |
| 312 | + UserStories: []UserStory{ |
| 313 | + {ID: "US-001", Title: "Low priority", Priority: 3, Passes: false}, |
| 314 | + {ID: "US-002", Title: "High priority", Priority: 1, Passes: false}, |
| 315 | + {ID: "US-003", Title: "Mid priority", Priority: 2, Passes: false}, |
| 316 | + }, |
| 317 | + } |
| 318 | + |
| 319 | + ctx := p.NextStoryContext() |
| 320 | + if ctx == nil { |
| 321 | + t.Fatal("expected non-nil context") |
| 322 | + } |
| 323 | + |
| 324 | + // Parse the JSON to verify it's the highest-priority story |
| 325 | + var story UserStory |
| 326 | + if err := json.Unmarshal([]byte(*ctx), &story); err != nil { |
| 327 | + t.Fatalf("failed to parse story context JSON: %v", err) |
| 328 | + } |
| 329 | + if story.ID != "US-002" { |
| 330 | + t.Errorf("expected highest-priority story US-002, got %s", story.ID) |
| 331 | + } |
| 332 | +} |
| 333 | + |
| 334 | +func TestPRD_NextStoryContext_ReturnsNilWhenAllComplete(t *testing.T) { |
| 335 | + p := &PRD{ |
| 336 | + Project: "Test", |
| 337 | + UserStories: []UserStory{ |
| 338 | + {ID: "US-001", Passes: true}, |
| 339 | + {ID: "US-002", Passes: true}, |
| 340 | + }, |
| 341 | + } |
| 342 | + |
| 343 | + ctx := p.NextStoryContext() |
| 344 | + if ctx != nil { |
| 345 | + t.Errorf("expected nil when all stories complete, got %q", *ctx) |
| 346 | + } |
| 347 | +} |
| 348 | + |
| 349 | +func TestPRD_NextStoryContext_SkipsPassingStories(t *testing.T) { |
| 350 | + p := &PRD{ |
| 351 | + Project: "Test", |
| 352 | + UserStories: []UserStory{ |
| 353 | + {ID: "US-001", Title: "Done", Priority: 1, Passes: true}, |
| 354 | + {ID: "US-002", Title: "Pending", Priority: 2, Passes: false}, |
| 355 | + }, |
| 356 | + } |
| 357 | + |
| 358 | + ctx := p.NextStoryContext() |
| 359 | + if ctx == nil { |
| 360 | + t.Fatal("expected non-nil context") |
| 361 | + } |
| 362 | + |
| 363 | + var story UserStory |
| 364 | + if err := json.Unmarshal([]byte(*ctx), &story); err != nil { |
| 365 | + t.Fatalf("failed to parse story context JSON: %v", err) |
| 366 | + } |
| 367 | + if story.ID != "US-002" { |
| 368 | + t.Errorf("expected US-002 (only pending story), got %s", story.ID) |
| 369 | + } |
| 370 | +} |
| 371 | + |
| 372 | +func TestPRD_NextStoryContext_EmptyPRD(t *testing.T) { |
| 373 | + p := &PRD{ |
| 374 | + Project: "Empty", |
| 375 | + UserStories: []UserStory{}, |
| 376 | + } |
| 377 | + |
| 378 | + ctx := p.NextStoryContext() |
| 379 | + if ctx != nil { |
| 380 | + t.Errorf("expected nil for empty PRD, got %q", *ctx) |
| 381 | + } |
| 382 | +} |
| 383 | + |
| 384 | +func TestPRD_NextStoryContext_ValidJSON(t *testing.T) { |
| 385 | + p := &PRD{ |
| 386 | + Project: "Test", |
| 387 | + UserStories: []UserStory{ |
| 388 | + { |
| 389 | + ID: "US-001", |
| 390 | + Title: "Test Story", |
| 391 | + Description: "A test description", |
| 392 | + AcceptanceCriteria: []string{"AC1", "AC2"}, |
| 393 | + Priority: 1, |
| 394 | + Passes: false, |
| 395 | + }, |
| 396 | + }, |
| 397 | + } |
| 398 | + |
| 399 | + ctx := p.NextStoryContext() |
| 400 | + if ctx == nil { |
| 401 | + t.Fatal("expected non-nil context") |
| 402 | + } |
| 403 | + |
| 404 | + var story UserStory |
| 405 | + if err := json.Unmarshal([]byte(*ctx), &story); err != nil { |
| 406 | + t.Fatalf("NextStoryContext did not return valid JSON: %v", err) |
| 407 | + } |
| 408 | + if story.ID != "US-001" { |
| 409 | + t.Errorf("expected ID US-001, got %s", story.ID) |
| 410 | + } |
| 411 | + if story.Title != "Test Story" { |
| 412 | + t.Errorf("expected title 'Test Story', got '%s'", story.Title) |
| 413 | + } |
| 414 | + if len(story.AcceptanceCriteria) != 2 { |
| 415 | + t.Errorf("expected 2 acceptance criteria, got %d", len(story.AcceptanceCriteria)) |
| 416 | + } |
| 417 | +} |
| 418 | + |
| 419 | +func TestPRD_NextStoryContext_PromptSizeUnder10KB(t *testing.T) { |
| 420 | + // Create a 300-story PRD to verify the context stays small |
| 421 | + stories := make([]UserStory, 300) |
| 422 | + for i := range stories { |
| 423 | + stories[i] = UserStory{ |
| 424 | + ID: fmt.Sprintf("US-%03d", i+1), |
| 425 | + Title: fmt.Sprintf("Story %d with a reasonably long title for realism", i+1), |
| 426 | + Description: "This is a description that is moderately long to simulate realistic PRD content for testing purposes.", |
| 427 | + AcceptanceCriteria: []string{"Criterion A", "Criterion B", "Criterion C"}, |
| 428 | + Priority: i + 1, |
| 429 | + Passes: i > 0, // Only first story is pending |
| 430 | + } |
| 431 | + } |
| 432 | + p := &PRD{ |
| 433 | + Project: "Large Project", |
| 434 | + Description: "A large PRD with 300 stories", |
| 435 | + UserStories: stories, |
| 436 | + } |
| 437 | + |
| 438 | + ctx := p.NextStoryContext() |
| 439 | + if ctx == nil { |
| 440 | + t.Fatal("expected non-nil context for 300-story PRD") |
| 441 | + } |
| 442 | + if len(*ctx) > 10*1024 { |
| 443 | + t.Errorf("story context is %d bytes, expected under 10KB", len(*ctx)) |
| 444 | + } |
| 445 | +} |
0 commit comments