| // Copyright 2024 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package rules |
| |
| import ( |
| "context" |
| "strings" |
| "testing" |
| |
| "golang.org/x/oscar/internal/github" |
| ) |
| |
| func TestIssue(t *testing.T) { |
| ctx := context.Background() |
| llm := &ruleTestGenerator{} |
| |
| // Construct a test issue. |
| i := new(github.Issue) |
| i.Number = 999 |
| i.User = github.User{Login: "user"} |
| i.Title = "title" |
| i.Body = "body" |
| |
| // Ask about rule violations. |
| r, err := Issue(ctx, llm, i) |
| if err != nil { |
| t.Fatalf("IssueRules failed with %v", err) |
| } |
| |
| // Check result. |
| if !strings.Contains(r.Response, "\n- The issue title must start") { |
| t.Errorf("expected the issue title rule failed, but it didn't. Total output: %s", r.Response) |
| } |
| if n := strings.Count(r.Response, "\n- "); n != 1 { |
| t.Errorf("wanted 1 rule violation, got %d", n) |
| } |
| } |
| |
| // Implements llm.TextGenerator |
| type ruleTestGenerator struct { |
| } |
| |
| func (g *ruleTestGenerator) Model() string { return "ruleTestGenerator" } |
| func (g *ruleTestGenerator) GenerateText(ctx context.Context, promptParts ...string) (string, error) { |
| req := strings.Join(promptParts, " ") |
| if strings.Contains(req, "Your job is to categorize") { |
| // categorize request. Always report it as a "bug". |
| return "bug\nI think this is a bug.", nil |
| } |
| if strings.Contains(req, "Your job is to decide") { |
| // rule request. Report that the title rule failed and the others succeeded. |
| if strings.Contains(req, "The issue title must start") { |
| return "no\nThe title is malformed.", nil |
| } |
| return "yes\nThe rule is obeyed.", nil |
| |
| } |
| return "UNK", nil |
| } |