-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_split_swmd.py
More file actions
936 lines (778 loc) · 37.7 KB
/
test_split_swmd.py
File metadata and controls
936 lines (778 loc) · 37.7 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
"""Tests for split_swmd.py using the example document."""
import os
import re
import shutil
import tempfile
import unittest
from split_swmd import (
ParsedDocument,
SectionContent,
_is_structural_heading,
_parse_heading,
build_business_logic,
build_swmd,
build_technical,
extract_program_name,
find_program_docs,
get_sort_key,
is_main_doc,
parse_document,
parse_footer,
parse_front_matter,
parse_preamble,
)
EXAMPLE_DOC = "/home/omerr/repos/test-data/example-client/.swm/example.sw.md"
WSNPT_DOC = "/home/omerr/repos/test-data/example-client/.swm/b12345-example-program-processing.wsnpt.sw.md"
SWM_DIR = "/home/omerr/repos/test-data/example-client/.swm"
def _count_source_section_br_rows(path: str) -> int:
"""Count | BR- | rows that belong to section-level Rule ID tables (Code Location column)."""
with open(path, "r", encoding="utf-8") as f:
lines = [l.rstrip("\n") for l in f]
pat = re.compile(r"^\|\s*Rule\s+ID\s*\|")
in_section_table = False
count = 0
for l in lines:
if pat.match(l):
in_section_table = "Code Location" in l
elif not l.strip().startswith("|"):
in_section_table = False
if "| BR-" in l and in_section_table:
count += 1
return count
def _load_lines(path: str) -> list[str]:
with open(path, "r", encoding="utf-8") as f:
return [l.rstrip("\n") for l in f.readlines()]
class TestFileDiscovery(unittest.TestCase):
def test_extract_program_name(self):
self.assertEqual(extract_program_name("b12345-some-module.sw.md"), "b12345")
self.assertEqual(extract_program_name("B12345-Name.sw.md"), "b12345")
def test_extract_program_name_numbered(self):
self.assertEqual(extract_program_name("prg001-1-xxx.sw.md"), "prg001")
self.assertEqual(extract_program_name("prg001-2-xxx.sw.md"), "prg001")
def test_is_main_doc(self):
self.assertTrue(is_main_doc("b12345-example-program.sw.md"))
self.assertFalse(is_main_doc("prg001-1-xxx.sw.md"))
self.assertFalse(is_main_doc("prg001-2-xxx.sw.md"))
self.assertTrue(is_main_doc("prg001-xxx.sw.md"))
def test_get_sort_key(self):
self.assertEqual(get_sort_key("b12345-name.sw.md"), 0)
self.assertEqual(get_sort_key("prg001-1-xxx.sw.md"), 1)
self.assertEqual(get_sort_key("prg001-2-xxx.sw.md"), 2)
def test_find_program_docs(self):
docs = find_program_docs(SWM_DIR, "b12345")
self.assertGreaterEqual(len(docs), 1)
for d in docs:
self.assertTrue(d.endswith(".sw.md"))
self.assertTrue(d.lower().startswith("b12345"))
class TestMetadataParsing(unittest.TestCase):
def test_front_matter(self):
lines = _load_lines(EXAMPLE_DOC)
fm, title, end_idx = parse_front_matter(lines)
self.assertIn("---", fm)
self.assertEqual(title, "B12345 - Example Program Processing")
self.assertGreater(end_idx, 0)
def test_front_matter_roundtrip(self):
lines = _load_lines(EXAMPLE_DOC)
fm, title, end_idx = parse_front_matter(lines)
self.assertTrue(fm.startswith("---"))
self.assertTrue(fm.endswith("---"))
self.assertIn("title:", fm)
def test_footer(self):
lines = _load_lines(EXAMPLE_DOC)
footer, footer_start = parse_footer(lines)
self.assertIn(" ", footer)
self.assertIn("auto-generated", footer)
self.assertIn("<SwmMeta", footer)
self.assertLess(footer_start, len(lines))
class TestPreambleExtraction(unittest.TestCase):
def setUp(self):
self.lines = _load_lines(EXAMPLE_DOC)
_, _, self.content_start = parse_front_matter(self.lines)
_, self.footer_start = parse_footer(self.lines)
def test_preamble_exists(self):
preamble, after = parse_preamble(
self.lines, self.content_start, self.footer_start
)
self.assertIsNotNone(preamble)
self.assertGreater(after, self.content_start)
def test_preamble_has_overview(self):
preamble, _ = parse_preamble(
self.lines, self.content_start, self.footer_start
)
self.assertIn("# Overview", preamble.raw)
def test_preamble_has_high_level_mermaid(self):
preamble, _ = parse_preamble(
self.lines, self.content_start, self.footer_start
)
self.assertIn("```mermaid", preamble.raw)
def test_preamble_has_dependencies(self):
preamble, _ = parse_preamble(
self.lines, self.content_start, self.footer_start
)
self.assertIn("Dependencies", preamble.raw)
self.assertIn("Programs", preamble.raw)
self.assertIn("Copybooks", preamble.raw)
def test_preamble_ends_at_workflow(self):
preamble, _ = parse_preamble(
self.lines, self.content_start, self.footer_start
)
self.assertTrue(preamble.raw.rstrip().endswith("# Workflow"))
class TestSectionBoundaryDetection(unittest.TestCase):
def setUp(self):
self.doc = parse_document(EXAMPLE_DOC)
def test_section_count(self):
# The example doc has many sections — verify a reasonable count
self.assertGreater(len(self.doc.sections), 10)
def test_no_structural_headings_as_sections(self):
structural = {"overview", "workflow", "dependencies", "programs", "copybooks"}
for section in self.doc.sections:
self.assertNotIn(
section.heading_text.lower(),
structural,
f"Structural heading found as section: {section.heading_text}",
)
def test_no_user_story_headings_as_sections(self):
for section in self.doc.sections:
self.assertFalse(
section.heading_text.lower().startswith("user stor"),
f"User story heading found as section: {section.heading_text}",
)
def test_first_section_is_starting_transaction_logic(self):
self.assertEqual(
self.doc.sections[0].heading_text,
"Starting the main transaction logic",
)
class TestSectionContentParsing(unittest.TestCase):
def setUp(self):
self.doc = parse_document(EXAMPLE_DOC)
# Find specific sections by name
self.sections_by_name = {}
for s in self.doc.sections:
self.sections_by_name[s.heading_text.lower()] = s
def test_first_section_has_mermaid(self):
first = self.doc.sections[0]
self.assertGreater(len(first.mermaid_blocks), 0)
self.assertIn("```mermaid", first.mermaid_blocks[0])
def test_first_section_has_description(self):
first = self.doc.sections[0]
self.assertGreater(len(first.description), 0)
self.assertIn("transaction", first.description.lower())
def test_first_section_has_business_rules(self):
first = self.doc.sections[0]
self.assertIn("Rule ID", first.business_rules_table)
# Count rules: number of "| BR-" occurrences
rule_count = first.business_rules_table.count("| BR-")
self.assertEqual(rule_count, 5, f"Expected 5 business rules, got {rule_count}")
def test_first_section_has_user_stories(self):
first = self.doc.sections[0]
self.assertIn("User Story", first.user_stories_block)
def test_first_section_has_snippets(self):
first = self.doc.sections[0]
self.assertGreater(len(first.snippet_blocks), 0)
def test_description_is_before_rules(self):
"""Description text should be pre-rules, not post-rules."""
first = self.doc.sections[0]
# Description should contain the section intro text
self.assertIn("initializ", first.description.lower())
# Description should NOT contain rule table content
self.assertNotIn("| BR-", first.description)
def test_business_rules_only_section_level(self):
"""Only section-level '| Rule ID |' tables, not user story tables."""
first = self.doc.sections[0]
# The business_rules_table should have the 7-column format
self.assertIn("Code Location", first.business_rules_table)
# User story tables have "Paragraph Name" instead
self.assertNotIn("Paragraph Name", first.business_rules_table)
def test_snippet_blocks_contain_swmsnippet(self):
first = self.doc.sections[0]
for block in first.snippet_blocks:
self.assertIn("<SwmSnippet", block)
self.assertIn("</SwmSnippet>", block)
def test_section_without_mermaid(self):
"""Some sections may not have mermaid blocks."""
restricted = self.sections_by_name.get("handling restricted system output")
if restricted:
# This section has rules but might not have a mermaid
self.assertIsInstance(restricted.mermaid_blocks, list)
class TestDocumentParsing(unittest.TestCase):
def test_parse_document(self):
doc = parse_document(EXAMPLE_DOC)
self.assertIsNotNone(doc)
self.assertEqual(doc.title, "B12345 - Example Program Processing")
self.assertIsNotNone(doc.preamble)
self.assertGreater(len(doc.sections), 0)
self.assertIn("<SwmMeta", doc.footer)
def test_all_snippets_accounted_for(self):
"""Every SwmSnippet in the source should be in some section's snippet_blocks."""
lines = _load_lines(EXAMPLE_DOC)
source_snippet_count = sum(1 for l in lines if "<SwmSnippet" in l and "path=" in l)
doc = parse_document(EXAMPLE_DOC)
parsed_snippet_count = 0
for s in doc.sections:
for block in s.snippet_blocks:
parsed_snippet_count += block.count("<SwmSnippet")
self.assertEqual(
parsed_snippet_count,
source_snippet_count,
f"Parsed {parsed_snippet_count} snippets but source has {source_snippet_count}",
)
class TestBusinessLogicOutput(unittest.TestCase):
def setUp(self):
self.doc = parse_document(EXAMPLE_DOC)
self.output = build_business_logic(self.doc.title, [self.doc])
def test_has_preamble(self):
self.assertIn("# Overview", self.output)
self.assertIn("Dependencies", self.output)
def test_has_descriptions(self):
self.assertIn("transaction", self.output.lower())
def test_has_flowcharts(self):
self.assertIn("```mermaid", self.output)
def test_has_rules(self):
self.assertIn("Rule ID", self.output)
def test_no_snippets(self):
self.assertNotIn("<SwmSnippet", self.output)
self.assertNotIn("</SwmSnippet>", self.output)
def test_no_user_stories(self):
self.assertNotIn("User Story", self.output)
def test_title(self):
self.assertTrue(self.output.startswith("# B12345"))
self.assertIn("Business Logic", self.output.split("\n")[0])
class TestTechnicalOutput(unittest.TestCase):
def setUp(self):
self.doc = parse_document(EXAMPLE_DOC)
self.output = build_technical(self.doc.title, [self.doc])
def test_has_snippets(self):
self.assertIn("<SwmSnippet", self.output)
self.assertIn("</SwmSnippet>", self.output)
def test_has_section_headings(self):
self.assertIn("# Starting the main transaction logic", self.output)
def test_no_descriptions(self):
"""Technical doc should not have description text (the pre-rules text)."""
# The description from the first section mentions initialization
# But walkthrough text (inside snippets) is allowed
lines = self.output.split("\n")
for line in lines:
if line.startswith("| Rule ID"):
self.fail("Technical doc should not contain business rules tables")
def test_no_flowcharts(self):
self.assertNotIn("```mermaid", self.output)
def test_no_rules(self):
self.assertNotIn("| Rule ID |", self.output)
def test_no_user_stories(self):
self.assertNotIn("User Story", self.output)
def test_title(self):
self.assertTrue(self.output.startswith("# B12345"))
self.assertIn("Technical Description", self.output.split("\n")[0])
def test_snippet_count_matches_source(self):
lines = _load_lines(EXAMPLE_DOC)
source_count = sum(1 for l in lines if "<SwmSnippet" in l and "path=" in l)
tech_count = self.output.count("<SwmSnippet")
self.assertEqual(tech_count, source_count)
class TestOutputFormat(unittest.TestCase):
def setUp(self):
self.doc = parse_document(EXAMPLE_DOC)
self.biz_content = build_business_logic(self.doc.title, [self.doc])
self.tech_content = build_technical(self.doc.title, [self.doc])
def test_md_has_no_front_matter(self):
# Plain .md starts with h1, not ---
self.assertTrue(self.biz_content.startswith("#"))
self.assertTrue(self.tech_content.startswith("#"))
def test_swmd_has_front_matter(self):
biz_swmd = build_swmd(
self.biz_content,
f"{self.doc.title} - Business Logic",
self.doc.footer,
)
self.assertTrue(biz_swmd.startswith("---"))
self.assertIn("title:", biz_swmd)
def test_swmd_has_footer(self):
biz_swmd = build_swmd(
self.biz_content,
f"{self.doc.title} - Business Logic",
self.doc.footer,
)
self.assertIn("<SwmMeta", biz_swmd)
self.assertIn("auto-generated", biz_swmd)
def test_swmd_no_duplicate_title(self):
"""The sw.md version should not have both front matter title and h1 title."""
biz_swmd = build_swmd(
self.biz_content,
f"{self.doc.title} - Business Logic",
self.doc.footer,
)
lines = biz_swmd.split("\n")
# After front matter (---...---), the next non-empty line should not be the h1 title
in_fm = False
past_fm = False
for line in lines:
if line.strip() == "---":
if not in_fm:
in_fm = True
else:
past_fm = True
continue
if past_fm and line.strip():
self.assertFalse(
line.startswith("# B12345") and "Business Logic" in line,
"sw.md should not duplicate the title as h1 after front matter",
)
break
class TestEndToEnd(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.tmpdir)
def test_end_to_end_parse_and_split(self):
doc = parse_document(EXAMPLE_DOC)
biz = build_business_logic(doc.title, [doc])
tech = build_technical(doc.title, [doc])
biz_path = os.path.join(self.tmpdir, "biz.md")
tech_path = os.path.join(self.tmpdir, "tech.md")
with open(biz_path, "w") as f:
f.write(biz)
with open(tech_path, "w") as f:
f.write(tech)
# Verify files exist and have content
self.assertGreater(os.path.getsize(biz_path), 0)
self.assertGreater(os.path.getsize(tech_path), 0)
# Read back and verify key properties
with open(biz_path) as f:
biz_text = f.read()
with open(tech_path) as f:
tech_text = f.read()
# Business logic: has rules, no snippets
self.assertGreater(biz_text.count("Rule ID"), 0)
self.assertEqual(biz_text.count("<SwmSnippet"), 0)
# Technical: has snippets, no rules
self.assertGreater(tech_text.count("<SwmSnippet"), 0)
self.assertEqual(tech_text.count("Rule ID"), 0)
# Neither has user stories
self.assertEqual(biz_text.count("User Story"), 0)
self.assertEqual(tech_text.count("User Story"), 0)
class TestMalformedHeadings(unittest.TestCase):
def test_malformed_heading_stripped(self):
"""Headings like '## ####### User Story 1:' should be detected as user stories."""
line = "## ####### User Story 1: Error context preparation"
result = _parse_heading(line)
self.assertIsNotNone(result)
level, text = result
self.assertEqual(level, 2)
self.assertTrue(text.startswith("User Story"), f"Text was: {text}")
self.assertTrue(_is_structural_heading(text))
def test_normal_heading_not_structural(self):
line = "## Handling restricted system output"
result = _parse_heading(line)
self.assertIsNotNone(result)
level, text = result
self.assertEqual(level, 2)
self.assertFalse(_is_structural_heading(text))
class TestMultiPartDocs(unittest.TestCase):
def test_sort_order(self):
files = [
"prog-2-extra.sw.md",
"prog-name.sw.md",
"prog-1-more.sw.md",
]
files.sort(key=get_sort_key)
self.assertEqual(files[0], "prog-name.sw.md") # main = 0
self.assertEqual(files[1], "prog-1-more.sw.md") # 1
self.assertEqual(files[2], "prog-2-extra.sw.md") # 2
def test_only_main_has_preamble(self):
"""Non-main docs should not contribute preamble."""
# Create a fake numbered doc to test
tmpdir = tempfile.mkdtemp()
try:
# Write a minimal "numbered" doc
numbered_path = os.path.join(tmpdir, "prog-1-part.sw.md")
with open(numbered_path, "w") as f:
f.write("---\ntitle: Prog Part 1\n---\n# Overview\nSome text\n# Workflow\n## Section A\nDescription\n")
doc = parse_document(numbered_path)
self.assertIsNone(doc.preamble)
# But it should still have sections
self.assertGreater(len(doc.sections), 0)
finally:
shutil.rmtree(tmpdir)
# =========================================================================
# Additional tests for client delivery QA
# =========================================================================
class TestContentCompleteness(unittest.TestCase):
"""Verify no content is silently lost during splitting."""
def setUp(self):
self.doc = parse_document(EXAMPLE_DOC)
self.biz = build_business_logic(self.doc.title, [self.doc])
self.tech = build_technical(self.doc.title, [self.doc])
def test_all_section_level_br_rows_in_biz(self):
"""Every BR- row from section-level Rule ID tables must appear in biz output."""
source_br = _count_source_section_br_rows(EXAMPLE_DOC)
biz_br = sum(1 for l in self.biz.split("\n") if "| BR-" in l)
self.assertEqual(biz_br, source_br,
f"Source has {source_br} section-level BR- rows, biz has {biz_br}")
def test_no_user_story_tables_in_biz(self):
"""Business logic doc must not contain user story 'Paragraph Name' tables."""
self.assertNotIn("Paragraph Name", self.biz)
def test_description_text_not_truncated(self):
"""Spot-check that full description text appears in biz output."""
for s in self.doc.sections:
if s.description:
# Check first 80 chars of description appear in output
snippet = s.description[:80]
self.assertIn(snippet, self.biz,
f"Description truncated for section '{s.heading_text}': "
f"expected '{snippet[:40]}...'")
def test_walkthrough_text_in_tech(self):
"""Walkthrough text between SwmSnippet blocks must appear in tech output."""
expected_phrases = [
"we kick off the transaction",
"we grab the address of the common work area",
"we grab the current time and format it as YYMMDD",
"SYSTEM-RESTRICTED sets a bunch of status",
]
for phrase in expected_phrases:
self.assertIn(phrase, self.tech,
f"Walkthrough text missing from tech doc: '{phrase}'")
def test_walkthrough_text_not_in_biz(self):
"""Walkthrough text (inside SwmSnippet blocks) must NOT appear in biz output."""
walkthrough_phrases = [
"we kick off the transaction by checking",
"we grab the address of the common work area so the transaction",
"SYSTEM-RESTRICTED sets a bunch of status and message fields",
]
for phrase in walkthrough_phrases:
self.assertNotIn(phrase, self.biz,
f"Walkthrough text leaked into biz doc: '{phrase}'")
def test_mermaid_blocks_fully_preserved(self):
"""Mermaid blocks must be complete (opening and closing fences)."""
mermaid_opens = self.biz.count("```mermaid")
mermaid_closes = 0
in_mermaid = False
for line in self.biz.split("\n"):
if line.strip().startswith("```mermaid"):
in_mermaid = True
elif in_mermaid and line.strip() == "```":
mermaid_closes += 1
in_mermaid = False
self.assertEqual(mermaid_opens, mermaid_closes,
f"Mismatched mermaid fences: {mermaid_opens} opens, {mermaid_closes} closes")
def test_every_section_appears_in_at_least_one_doc(self):
"""Every parsed section heading must appear in biz, tech, or both."""
for s in self.doc.sections:
in_biz = s.heading_line in self.biz
in_tech = s.heading_line in self.tech
self.assertTrue(in_biz or in_tech,
f"Section '{s.heading_text}' missing from both outputs")
class TestSectionsWithoutUserStories(unittest.TestCase):
"""Sections that go directly from rules to snippets (no user stories)."""
def setUp(self):
self.doc = parse_document(EXAMPLE_DOC)
self.sections_by_name = {s.heading_text.lower(): s for s in self.doc.sections}
def test_processing_additional_records_no_user_stories(self):
s = self.sections_by_name.get("processing additional records and search")
self.assertIsNotNone(s, "Section 'Processing additional records and search' not found")
self.assertEqual(s.user_stories_block, "")
self.assertGreater(len(s.business_rules_table), 0, "Should have business rules")
self.assertGreater(len(s.snippet_blocks), 0, "Should have snippets")
self.assertIn("<SwmSnippet", s.snippet_blocks[0])
def test_opening_database_no_user_stories(self):
s = self.sections_by_name.get("opening database for name search")
self.assertIsNotNone(s, "Section 'Opening database for name search' not found")
self.assertEqual(s.user_stories_block, "")
self.assertGreater(len(s.business_rules_table), 0, "Should have business rules")
self.assertGreater(len(s.snippet_blocks), 0, "Should have snippets")
def test_rules_to_snippets_sections_in_both_outputs(self):
"""These sections should appear in BOTH biz and tech docs."""
biz = build_business_logic(self.doc.title, [self.doc])
tech = build_technical(self.doc.title, [self.doc])
for name in ["processing additional records and search",
"opening database for name search"]:
s = self.sections_by_name[name]
self.assertIn(s.heading_line, biz,
f"'{name}' heading missing from business logic doc")
self.assertIn(s.heading_line, tech,
f"'{name}' heading missing from technical doc")
class TestEmptySections(unittest.TestCase):
"""Verify sections with partial content don't produce broken output."""
def test_section_without_mermaid_has_no_orphan_heading_in_biz(self):
"""Sections without mermaid but with description/rules should render properly."""
doc = parse_document(EXAMPLE_DOC)
biz = build_business_logic(doc.title, [doc])
for s in doc.sections:
if not s.mermaid_blocks and (s.description or s.business_rules_table):
# Heading should be present
self.assertIn(s.heading_line, biz)
# Should have content after the heading (not just the heading alone)
idx = biz.index(s.heading_line)
after = biz[idx + len(s.heading_line):].lstrip("\n")
# Next non-empty content should not be another heading
first_line = after.split("\n")[0].strip()
self.assertFalse(
first_line.startswith("#") and first_line != "",
f"Section '{s.heading_text}' has orphan heading in biz doc "
f"(next line: '{first_line[:50]}')"
)
def test_synthetic_section_no_snippets_skipped_in_tech(self):
"""A section with only description (no snippets) should be skipped in tech doc."""
doc = ParsedDocument(
title="Test",
front_matter="---\ntitle: Test\n---",
preamble=None,
sections=[
SectionContent(
heading_line="## Only Description",
heading_level=2,
heading_text="Only Description",
description="Some text here.",
),
SectionContent(
heading_line="## Has Snippets",
heading_level=2,
heading_text="Has Snippets",
snippet_blocks=["<SwmSnippet path=\"x\" line=\"1\">\ncode\n</SwmSnippet>"],
),
],
footer="",
source_file="test.sw.md",
)
tech = build_technical(doc.title, [doc])
self.assertNotIn("Only Description", tech)
self.assertIn("Has Snippets", tech)
def test_synthetic_section_no_biz_content_skipped_in_biz(self):
"""A section with only snippets (no desc/rules/mermaid) should be skipped in biz doc."""
doc = ParsedDocument(
title="Test",
front_matter="---\ntitle: Test\n---",
preamble=None,
sections=[
SectionContent(
heading_line="## Only Snippets",
heading_level=2,
heading_text="Only Snippets",
snippet_blocks=["<SwmSnippet path=\"x\" line=\"1\">\ncode\n</SwmSnippet>"],
),
SectionContent(
heading_line="## Has Description",
heading_level=2,
heading_text="Has Description",
description="Some text.",
),
],
footer="",
source_file="test.sw.md",
)
biz = build_business_logic(doc.title, [doc])
self.assertNotIn("Only Snippets", biz)
self.assertIn("Has Description", biz)
class TestUserStoryExclusion(unittest.TestCase):
"""User story content — including --- separators — must never leak into output."""
def setUp(self):
self.doc = parse_document(EXAMPLE_DOC)
self.biz = build_business_logic(self.doc.title, [self.doc])
self.tech = build_technical(self.doc.title, [self.doc])
def test_story_description_excluded_from_both(self):
self.assertNotIn("Story Description", self.biz)
self.assertNotIn("Story Description", self.tech)
def test_relevant_functionality_excluded_from_both(self):
self.assertNotIn("Relevant Functionality", self.biz)
self.assertNotIn("Relevant Functionality", self.tech)
def test_business_rule_mapping_excluded_from_both(self):
"""The 'Business Rule Mapping' tables inside user stories must be excluded."""
self.assertNotIn("Business Rule Mapping", self.biz)
self.assertNotIn("Business Rule Mapping", self.tech)
def test_user_story_hr_separators_not_in_biz(self):
"""Business logic doc should have zero --- lines (all --- come from user stories or snippets)."""
hr_count = sum(1 for l in self.biz.split("\n") if l.strip() == "---")
self.assertEqual(hr_count, 0,
f"Business logic doc has {hr_count} --- lines (should be 0)")
def test_user_story_hr_separators_in_tech_only_from_snippets(self):
"""In the tech doc, --- lines should only appear inside SwmSnippet blocks."""
lines = self.tech.split("\n")
in_snippet = False
stray_hr = []
for i, line in enumerate(lines):
if "<SwmSnippet" in line:
in_snippet = True
elif "</SwmSnippet>" in line:
in_snippet = False
elif line.strip() == "---" and not in_snippet:
stray_hr.append(i + 1)
self.assertEqual(len(stray_hr), 0,
f"Tech doc has {len(stray_hr)} --- lines outside SwmSnippet blocks "
f"at lines: {stray_hr[:5]}")
def test_user_stories_block_captures_hr_separators(self):
"""User stories blocks should contain --- separators (confirming they're captured, not lost)."""
sections_with_stories = [s for s in self.doc.sections if s.user_stories_block]
self.assertGreater(len(sections_with_stories), 0)
stories_with_hr = sum(1 for s in sections_with_stories if "---" in s.user_stories_block)
self.assertEqual(stories_with_hr, len(sections_with_stories),
"Not all user stories blocks contain --- separators")
class TestContentRoundTrip(unittest.TestCase):
"""Verify that biz + tech + excluded content accounts for all source content."""
def setUp(self):
self.doc = parse_document(EXAMPLE_DOC)
self.biz = build_business_logic(self.doc.title, [self.doc])
self.tech = build_technical(self.doc.title, [self.doc])
def test_all_section_headings_accounted_for(self):
"""Every section heading should appear in at least one output document."""
for s in self.doc.sections:
in_biz = s.heading_line in self.biz
in_tech = s.heading_line in self.tech
self.assertTrue(in_biz or in_tech,
f"Section heading not in any output: '{s.heading_text}'")
def test_all_rule_id_headers_in_biz(self):
"""The number of Rule ID table headers in biz should match section-level tables."""
lines = _load_lines(EXAMPLE_DOC)
pat = re.compile(r"^\|\s*Rule\s+ID\s*\|")
source_section_headers = sum(
1 for l in lines if pat.match(l) and "Code Location" in l
)
biz_headers = sum(1 for l in self.biz.split("\n") if pat.match(l))
self.assertEqual(biz_headers, source_section_headers)
def test_combined_snippet_count(self):
"""Snippets in tech should equal snippets in source (none lost, none duplicated)."""
lines = _load_lines(EXAMPLE_DOC)
source_opens = sum(1 for l in lines if "<SwmSnippet" in l and "path=" in l)
tech_opens = self.tech.count("<SwmSnippet")
biz_opens = self.biz.count("<SwmSnippet")
self.assertEqual(biz_opens, 0, "Biz doc should have 0 snippets")
self.assertEqual(tech_opens, source_opens,
f"Tech has {tech_opens} snippets, source has {source_opens}")
def test_heading_levels_preserved(self):
"""Original heading levels must be preserved in output documents."""
for s in self.doc.sections:
expected_prefix = "#" * s.heading_level + " "
if s.heading_line in self.biz:
idx = self.biz.index(s.heading_line)
actual_line = self.biz[idx:idx + len(s.heading_line)]
self.assertTrue(actual_line.startswith(expected_prefix),
f"Heading level changed for '{s.heading_text}': "
f"expected h{s.heading_level}")
def test_section_order_preserved(self):
"""Sections should appear in the same order as the source document."""
biz_positions = []
tech_positions = []
for s in self.doc.sections:
if s.heading_line in self.biz:
biz_positions.append(self.biz.index(s.heading_line))
if s.heading_line in self.tech:
tech_positions.append(self.tech.index(s.heading_line))
self.assertEqual(biz_positions, sorted(biz_positions),
"Section order not preserved in business logic doc")
self.assertEqual(tech_positions, sorted(tech_positions),
"Section order not preserved in technical doc")
class TestWsnptProductionFile(unittest.TestCase):
"""Run all critical checks against the actual production file (10678 lines)."""
@classmethod
def setUpClass(cls):
cls.doc = parse_document(WSNPT_DOC)
cls.biz = build_business_logic(cls.doc.title, [cls.doc])
cls.tech = build_technical(cls.doc.title, [cls.doc])
def test_zero_snippets_in_biz(self):
self.assertEqual(self.biz.count("<SwmSnippet"), 0)
def test_snippets_in_tech_match_source(self):
lines = _load_lines(WSNPT_DOC)
source_count = sum(1 for l in lines if "<SwmSnippet" in l and "path=" in l)
self.assertEqual(self.tech.count("<SwmSnippet"), source_count)
def test_zero_user_stories_in_biz(self):
self.assertEqual(self.biz.count("User Story"), 0)
def test_zero_user_stories_in_tech(self):
self.assertEqual(self.tech.count("User Story"), 0)
def test_zero_rules_in_tech(self):
self.assertEqual(self.tech.count("Rule ID"), 0)
def test_rules_present_in_biz(self):
self.assertGreater(self.biz.count("Rule ID"), 0)
def test_mermaid_in_biz_not_tech(self):
self.assertGreater(self.biz.count("```mermaid"), 0)
self.assertEqual(self.tech.count("```mermaid"), 0)
def test_br_rows_match_source(self):
source_br = _count_source_section_br_rows(WSNPT_DOC)
biz_br = sum(1 for l in self.biz.split("\n") if "| BR-" in l)
self.assertEqual(biz_br, source_br)
def test_preamble_includes_extended_content(self):
"""The wsnpt file has additional preamble sections not in example.sw.md."""
self.assertIn("Direct Program Dependencies", self.biz)
self.assertIn("Where is this program used", self.biz)
self.assertIn("Input Details", self.biz)
self.assertIn("Database Segments", self.biz)
self.assertIn("Dead Code", self.biz)
def test_no_story_description_leak(self):
self.assertNotIn("Story Description", self.biz)
self.assertNotIn("Story Description", self.tech)
def test_no_relevant_functionality_leak(self):
self.assertNotIn("Relevant Functionality", self.biz)
self.assertNotIn("Relevant Functionality", self.tech)
def test_no_business_rule_mapping_leak(self):
self.assertNotIn("Business Rule Mapping", self.biz)
self.assertNotIn("Business Rule Mapping", self.tech)
def test_section_count_matches_example(self):
"""Both files should produce the same number of sections (same workflow content)."""
example_doc = parse_document(EXAMPLE_DOC)
self.assertEqual(len(self.doc.sections), len(example_doc.sections))
def test_no_paragraph_name_tables_in_biz(self):
self.assertNotIn("Paragraph Name", self.biz)
def test_heading_levels_preserved(self):
for s in self.doc.sections:
expected_prefix = "#" * s.heading_level + " "
self.assertTrue(s.heading_line.startswith(expected_prefix),
f"h{s.heading_level} expected for '{s.heading_text}'")
def test_all_snippet_close_tags_in_tech(self):
opens = self.tech.count("<SwmSnippet")
closes = self.tech.count("</SwmSnippet>")
self.assertEqual(opens, closes,
f"Mismatched snippet tags: {opens} opens, {closes} closes")
def test_swmd_output_format(self):
biz_swmd = build_swmd(
self.biz,
f"{self.doc.title} - Business Logic",
self.doc.footer,
)
self.assertTrue(biz_swmd.startswith("---"))
self.assertIn("Business Logic", biz_swmd.split("\n")[1])
self.assertIn("<SwmMeta", biz_swmd)
class TestEndToEndCLI(unittest.TestCase):
"""End-to-end test through the CLI on the actual production file."""
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
self.script = os.path.join(os.path.dirname(__file__), "split_swmd.py")
def tearDown(self):
shutil.rmtree(self.tmpdir)
def test_cli_produces_four_files(self):
import subprocess
result = subprocess.run(
["python3", self.script,
"--swm-dir", SWM_DIR,
"--program", "b12345",
"--output-dir", self.tmpdir],
capture_output=True, text=True,
)
self.assertEqual(result.returncode, 0, f"CLI failed: {result.stderr}")
expected = [
"b12345-business-logic.md",
"b12345-business-logic.sw.md",
"b12345-technical.md",
"b12345-technical.sw.md",
]
for fname in expected:
path = os.path.join(self.tmpdir, fname)
self.assertTrue(os.path.exists(path), f"Missing output file: {fname}")
self.assertGreater(os.path.getsize(path), 0, f"Empty output file: {fname}")
def test_cli_swmd_has_front_matter_and_footer(self):
import subprocess
subprocess.run(
["python3", self.script,
"--swm-dir", SWM_DIR,
"--program", "b12345",
"--output-dir", self.tmpdir],
capture_output=True, text=True,
)
for suffix in ["business-logic", "technical"]:
swmd_path = os.path.join(self.tmpdir, f"b12345-{suffix}.sw.md")
with open(swmd_path) as f:
content = f.read()
self.assertTrue(content.startswith("---"), f"{suffix}.sw.md missing front matter")
self.assertIn("<SwmMeta", content, f"{suffix}.sw.md missing footer")
md_path = os.path.join(self.tmpdir, f"b12345-{suffix}.md")
with open(md_path) as f:
content = f.read()
self.assertTrue(content.startswith("#"), f"{suffix}.md should start with heading")
self.assertNotIn("<SwmMeta", content, f"{suffix}.md should not have footer")
if __name__ == "__main__":
unittest.main()