import io, os

WT = r"C:\Users\decid\Documents\projects\spt-core\.worktrees\rca-head"
P = os.path.join(WT, ".config", "nextest.toml")

orig = io.open(P, 'rb').read()
n = len(orig)

# Terminator measured, not assumed: this working copy is CRLF while the blob is LF.
EOL = b"\r\n" if orig.count(b"\r\n") > 0 else b"\n"
print("working copy EOL:", repr(EOL), "CR", orig.count(b"\r"), "LF", orig.count(b"\n"))

note_lines = [
    b"# DIAGNOSTIC-BRANCH ONLY (releases#294 poll-margin comparison). nextest's default",
    b"# success-output is `never`, which SUPPRESSES the IR95 instrumentation lines on exactly",
    b"# the path a passing cell takes -- the run would then answer pass/fail and yield NO",
    b"# margins, which is the whole point of the comparison. Set on BOTH platform profiles so",
    b"# the two arms differ in nothing but their tree. NOT for main.",
    b'success-output = "immediate"',
]
note = EOL.join(note_lines) + EOL

a1 = b'slow-timeout = { period = "60s", terminate-after = 4 }' + EOL
a2 = b'test-threads = 8' + EOL
assert orig.count(a1) == 1, ("default anchor", orig.count(a1))
assert orig.count(a2) == 1, ("ci-windows anchor", orig.count(a2))

new = orig.replace(a1, a1 + note, 1).replace(a2, a2 + note, 1)
assert new.count(b'success-output = "immediate"') == 2
io.open(P, 'wb').write(new)
print("nextest.toml", n, "->", len(new), "bytes")

section = None
seen = {}
for line in io.open(P, 'r', encoding='utf-8'):
    s = line.strip()
    if s.startswith('[') and not s.startswith('[['):
        section = s
    if s.startswith('success-output'):
        seen[section] = s
for k, v in sorted(seen.items()):
    print("  ", k, "->", v)
assert set(seen) == {'[profile.default]', '[profile.ci-windows]'}, seen
print("OK: exactly the two intended profiles")
