File size: 2,578 Bytes
b5c0df4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * Property-based tests for saved response sidebar truncation.
 *
 * **Validates: Requirements 8.3**
 *
 * Property 5: Saved response sidebar truncation — for any question string
 * exceeding 80 characters, the text displayed in the sidebar SHALL be between
 * 75 and 85 characters long (inclusive). Strings ≤ 80 characters are returned
 * unchanged.
 */

import { describe, it, expect } from "vitest";
import * as fc from "fast-check";
import { truncateQuestion } from "./Sidebar";

// ---------------------------------------------------------------------------
// Property 5: Saved response sidebar truncation
// ---------------------------------------------------------------------------

describe("Property 5: Saved response sidebar truncation", () => {
  /**
   * **Validates: Requirements 8.3**
   *
   * For any question string exceeding 80 characters, truncateQuestion returns
   * a string within [75, 85] characters long (inclusive).
   */
  it("truncated text is within [75, 85] characters when original exceeds 80 characters", () => {
    fc.assert(
      fc.property(
        fc.string({ minLength: 81, maxLength: 500 }),
        (question) => {
          const truncated = truncateQuestion(question);

          expect(truncated.length).toBeGreaterThanOrEqual(75);
          expect(truncated.length).toBeLessThanOrEqual(85);
        }
      )
    );
  });

  it("truncated text ends with ellipsis when original exceeds 80 characters", () => {
    fc.assert(
      fc.property(
        fc.string({ minLength: 81, maxLength: 500 }),
        (question) => {
          const truncated = truncateQuestion(question);

          expect(truncated.endsWith("\u2026")).toBe(true);
        }
      )
    );
  });

  it("strings of 80 characters or fewer are returned unchanged", () => {
    fc.assert(
      fc.property(
        fc.string({ minLength: 0, maxLength: 80 }),
        (question) => {
          const truncated = truncateQuestion(question);

          expect(truncated).toBe(question);
        }
      )
    );
  });

  it("truncated text is a prefix of the original (minus ellipsis)", () => {
    fc.assert(
      fc.property(
        fc.string({ minLength: 81, maxLength: 500 }),
        (question) => {
          const truncated = truncateQuestion(question);
          // Remove the trailing ellipsis to get the prefix
          const prefix = truncated.slice(0, -1);

          // The prefix must be a substring of the original starting at index 0
          expect(question.startsWith(prefix.trimEnd())).toBe(true);
        }
      )
    );
  });
});