diff --git a/backend/internal/domain/assessment_watermark_test.go b/backend/internal/domain/assessment_watermark_test.go index b60de54..e183752 100644 --- a/backend/internal/domain/assessment_watermark_test.go +++ b/backend/internal/domain/assessment_watermark_test.go @@ -1,6 +1,7 @@ package domain import ( + "fmt" "strings" "testing" "unicode" @@ -262,6 +263,43 @@ func TestAlignmentIsLongestForTheVariantThatWasLeakedAndShorterForOtherVariants( } } +// dependencyOrderStatement is a family whose first interchangeable phrase comes +// after the size of the task, so the piece before it differs between variants and +// the closing sentence is the only text that every variant shares. +func dependencyOrderStatement(jobs int, dependencies string) string { + return fmt.Sprintf("A worker runs %[1]d jobs numbered from 1 to %[1]d, one at a time. A dependency A->B means job A must finish before job B starts. "+ + "The dependencies are: %[2]s. The dependencies contain no cycle, so at least one valid order exists. "+ + "Among all execution orders that respect every dependency, choose the one that is smallest when compared number by number from the first position. "+ + "Write that order as job numbers separated by single spaces.", jobs, dependencies) +} + +func TestAlignmentDoesNotTakeASharedClosingSentenceForACopyOfAnotherVariant(t *testing.T) { + leaked, other := dependencyOrderStatement(9, "7->2 9->4 2->9 6->2"), dependencyOrderStatement(5, "3->4 2->5 2->4 2->1 1->4") + leak := "Posted in the group chat: " + VisibleAssessmentText(testWatermark(t, 1).Mark(leaked)) + " please help" + + // The closing sentence is long enough to be a landmark and occurs in both + // variants, but the leak differs from the other variant before it. + if got := AssessmentLeakAlignment(other, leak); got.Follows() || got.Runes != 0 { + t.Fatalf("another variant aligned %+v, want nothing: the text before the shared closing sentence differs", got) + } + if got := AssessmentLeakAlignment(leaked, leak); !got.Complete || !got.Follows() { + t.Fatalf("the leaked variant aligned %+v, want it followed to its end", got) + } + // A leak that starts inside the statement, at the closing sentence, still + // follows it: the part that the leak holds is a copy. + visible := VisibleAssessmentText(testWatermark(t, 1).Mark(leaked)) + closing := strings.Index(visible, " execution orders") + if closing < 0 { + t.Fatal("the leaked statement has no closing sentence") + } + tail := visible[strings.LastIndex(visible[:closing], ". ")+len(". "):] + for name, statement := range map[string]string{"leaked": leaked, "other": other} { + if got := AssessmentLeakAlignment(statement, tail); !got.Complete || !got.Follows() { + t.Fatalf("%s variant aligned %+v for a leak of the closing sentence only, want a copy of that part", name, got) + } + } +} + func TestSplittingATextAtItsPhrasesLeavesPiecesThatEveryCopyShares(t *testing.T) { statement := rateLimitVariant(t).Statement // Whatever phrases an attempt picked, the pieces between them are the same. diff --git a/backend/internal/domain/assessment_wording.go b/backend/internal/domain/assessment_wording.go index 8e2515b..10d0c91 100644 --- a/backend/internal/domain/assessment_wording.go +++ b/backend/internal/domain/assessment_wording.go @@ -194,18 +194,20 @@ func (alignment LeakAlignment) Follows() bool { return alignment.Complete || ali // AssessmentLeakAlignment aligns a leaked text with a stored statement. It finds // a landmark of the statement in the leak (the first piece between phrases that -// is long enough) and then follows the statement: after each piece, the leak -// must continue with one form of the next phrase and then the next piece. It -// stops at the first place where the leak does not follow the statement, so a -// leak that holds only part of the statement, an edited one, or the statement of -// another variant yields only the part that was aligned. +// is long enough), checks that the text before it agrees with the statement, and +// then follows the statement: after each piece, the leak must continue with one +// form of the next phrase and then the next piece. It stops at the first place +// where the leak does not follow the statement, so a leak that holds only part of +// the statement, an edited one, or the statement of another variant yields only +// the part that was aligned. A leak that differs before its landmark yields +// nothing. func AssessmentLeakAlignment(statement, leak string) LeakAlignment { normalized := NormalizedAssessmentText(statement) occurrences := findWordingOccurrences(normalized) pieces := AssessmentTextPiecesBetweenPhrases(normalized) leak = NormalizedAssessmentText(leak) start, position := firstLandmark(pieces, leak) - if start < 0 { + if start < 0 || !leakAgreesBeforeLandmark(normalized, occurrences, pieces, start, leak, position) { return LeakAlignment{} } alignment := LeakAlignment{Runes: utf8.RuneCountInString(pieces[start]), Complete: true} @@ -231,6 +233,24 @@ func AssessmentLeakAlignment(statement, leak string) LeakAlignment { return alignment } +// leakAgreesBeforeLandmark checks the text before the landmark, which the +// forward alignment never reads. The leak may start inside the statement or have +// other words before it, so the statement and the leak, both written up to the end +// of the landmark piece with their phrases in canonical form, must end with one +// another. A leak that merely shares a later piece with the statement, such as +// the closing sentence of a whole family of tasks, differs from the statement +// before that piece, in the numbers of its variant: it is the statement of +// another variant and not a copy. +func leakAgreesBeforeLandmark(statement string, occurrences []wordingOccurrence, pieces []string, landmark int, leak string, position int) bool { + landmarkStart := 0 + if landmark > 0 { + landmarkStart = occurrences[landmark-1].end + } + statementHead := canonicalWording(statement[:landmarkStart+len(pieces[landmark])]) + leakHead := canonicalWording(leak[:position]) + return strings.HasSuffix(statementHead, leakHead) || strings.HasSuffix(leakHead, statementHead) +} + // leakEndsInside reports whether the rest of the leak is the beginning of what // the statement has next (a form of the phrase, then the next piece), which is // how a leak that was cut ends. An empty rest, and a rest that holds only a cut diff --git a/backend/internal/usecase/assessment/leak_trace_test.go b/backend/internal/usecase/assessment/leak_trace_test.go index 17c5dcf..8b4bbe0 100644 --- a/backend/internal/usecase/assessment/leak_trace_test.go +++ b/backend/internal/usecase/assessment/leak_trace_test.go @@ -331,6 +331,37 @@ func TestTheLeakOfADeletedAttemptIsNotAttributedToAnotherVariantOfItsFamily(t *t } } +// A family whose first phrase follows the size of the task: the text before the +// phrase differs between variants, and only the closing sentence is shared. +func dependencyOrderStatement(jobs int, dependencies string) string { + return fmt.Sprintf("A worker runs %[1]d jobs numbered from 1 to %[1]d, one at a time. A dependency A->B means job A must finish before job B starts. "+ + "The dependencies are: %[2]s. The dependencies contain no cycle, so at least one valid order exists. "+ + "Among all execution orders that respect every dependency, choose the one that is smallest when compared number by number from the first position. "+ + "Write that order as job numbers separated by single spaces.", jobs, dependencies) +} + +func TestTheLeakOfADeletedAttemptIsNotAttributedToAVariantThatSharesOnlyTheClosingSentence(t *testing.T) { + repository := &leakRepositoryFake{} + deleted, left := domain.AssessmentAttemptID(uuid.New()), domain.AssessmentAttemptID(uuid.New()) + // The attempt that is left was served the same family with other numbers, so + // the closing sentence of the leak is the only text that its statement shares. + repository.serve(left, dependencyOrderStatement(5, "3->4 2->5 2->4 2->1 1->4")) + repository.serve(left, dependencyOrderStatement(8, "6->2 4->6 2->7 8->4")) + tracer := newLeakTracer(t, repository) + + leak := domain.VisibleAssessmentText(leakWatermark(t, deleted).Mark(dependencyOrderStatement(9, "7->2 9->4 2->9 6->2"))) + result := traceAs(t, tracer, leak, "") + if result.Status != domain.AssessmentLeakTraceNotFound || !result.AttemptID.IsZero() { + t.Fatalf("result = %+v, want not found: the statements that are left differ from the leak before their shared closing sentence", result) + } + // The attempt that holds the leaked variant is still found among the others. + repository.serve(deleted, dependencyOrderStatement(9, "7->2 9->4 2->9 6->2")) + found := traceAs(t, tracer, leak, "") + if found.Status != domain.AssessmentLeakTraceFound || found.AttemptID != deleted || found.Candidates != 1 { + t.Fatalf("result = %+v, want the attempt that holds the leaked variant and none of the others", found) + } +} + func TestACutLeakIsFoundButOtherWordsInsideTheStatementAreNot(t *testing.T) { repository := &leakRepositoryFake{} ids := attemptsThatWriteTheStatementDifferently(t, 1)