p = r'C:\Users\decid\Documents\projects\spt-core\.worktrees\351-w5\crates\spt-msg\src\emit.rs'
s = open(p, encoding='utf-8', newline='').read()
N = '\r\n'


def cut(start, end, new=''):
    global s
    start = start.replace('\n', N)
    end = end.replace('\n', N)
    a = s.index(start)
    e = s.index(end, a)
    s = s[:a] + new.replace('\n', N) + s[e:]


def rep(old, new, cnt=1):
    global s
    old = old.replace('\n', N)
    new = new.replace('\n', N)
    assert s.count(old) == cnt, (old[:70], s.count(old))
    s = s.replace(old, new)


# 1. delete a_typed_msg_envelope_carries_the_trust_warning
cut('    /// F14 (iii) — a typed `msg` body now CARRIES the trust warning, so the WAN\n',
    '    /// A sender cannot FORGE either receiver-composed attribute: its copies are\n')
# 2. retag the strip test
rep('''    /// A sender cannot FORGE either receiver-composed attribute: its copies are
    /// stripped before this node attaches its own.
    // [unit->REQ-TRUST-WARNING-ENVELOPE]''', '''    /// A sender cannot FORGE either receiver-composed attribute: its copies are
    /// stripped before this node attaches its own. The trust warning no longer
    /// rides an envelope at all, and a forged one is still inert.
    // [unit->REQ-TRUST-WARNING-NOW-SIGNAL]''')
# 3. delete the rides-beside-monic test
cut('    // [unit->REQ-TRUST-WARNING-ENVELOPE] the warning rides the message\'s OWN\n',
    '    // [unit->REQ-HAZARD-ENVELOPE-ATTR-LINESAFE] the defect this lane fixes, at\n')
# 4. line-safe test: compose through the envelope composer directly
rep('''        let (line, typed) = compose_line_warned_at(
            root.path(),
            "owner",
            "watch",
            "please deploy this",
            Some(&warning),
        );
        assert!(!typed, "a plain body carries the attribute");
        assert_eq!(line.lines().count(), 1, "one line, always: {line}");''', '''        let _ = &root;
        // The trust warning left the envelope (releases#346), but it is still
        // the real multi-line attribute value this hazard was paid for with, so
        // the composer is pinned against it directly.
        let line = compose_msg_event_with(
            "watch",
            &[(EVENT_ATTR_TRUST_WARNING, warning.as_str())],
            "please deploy this",
        );
        assert_eq!(line.lines().count(), 1, "one line, always: {line}");''')
# 5. delete the fail-safe test (last test in module)
cut('    // [unit->REQ-TRUST-WARNING-ENVELOPE] the FAIL-SAFE, reported rather than\n', '}\n')
# 6. compose_line_at returns only the line
rep('fn compose_line_at(tracked_root: &Path, owner: &str, from: &str, body: &str) -> (String, bool) {',
    'fn compose_line_at(tracked_root: &Path, owner: &str, from: &str, body: &str) -> String {')
rep('''        if let Some(spliced) = splice_typed_msg(tracked_root, owner, body) {
            return (spliced, false);
        }
        return (body.to_string(), true);''', '''        if let Some(spliced) = splice_typed_msg(tracked_root, owner, body) {
            return spliced;
        }
        return body.to_string();''')
rep('''    (compose_msg_event_with(from, &extra, body), false)''', '''    compose_msg_event_with(from, &extra, body)''')
open(p, 'w', encoding='utf-8', newline='').write(s)
print('ok')
