@@ -2,75 +2,66 @@ import assert from "node:assert/strict";
22import test from "node:test" ;
33import { buildBatchTemplate , parseBatchOperationCount } from "../../src/commands/batchApply.js" ;
44
5- test ( "buildBatchTemplate returns valid JSON with three operations" , ( ) => {
5+ test ( "buildBatchTemplate returns line-oriented format with three operations" , ( ) => {
66 const template = buildBatchTemplate ( ) ;
7- const parsed = JSON . parse ( template ) ;
7+ const lines = template . split ( "\n" ) . filter ( ( line ) => line . trim ( ) . length > 0 ) ;
88
9- assert . ok ( Array . isArray ( parsed . operations ) ) ;
10- assert . equal ( parsed . operations . length , 3 ) ;
11- assert . equal ( parsed . operations [ 0 ] . op , "replace" ) ;
12- assert . equal ( parsed . operations [ 1 ] . op , "tidy" ) ;
13- assert . equal ( parsed . operations [ 2 ] . op , "doc-set" ) ;
9+ assert . equal ( lines . length , 3 ) ;
10+ assert . ok ( lines [ 0 ] . startsWith ( "replace " ) , "first line should be a replace operation" ) ;
11+ assert . ok ( lines [ 1 ] . startsWith ( "doc.set " ) , "second line should be a doc.set operation" ) ;
12+ assert . ok ( lines [ 2 ] . startsWith ( "tidy.fix " ) , "third line should be a tidy.fix operation" ) ;
1413} ) ;
1514
1615test ( "buildBatchTemplate ends with a newline" , ( ) => {
1716 const template = buildBatchTemplate ( ) ;
1817 assert . ok ( template . endsWith ( "\n" ) ) ;
1918} ) ;
2019
21- test ( "parseBatchOperationCount counts operations in valid plan" , ( ) => {
22- const plan = JSON . stringify ( {
23- operations : [
24- { op : "replace" , file : "a.txt" , from : "x" , to : "y" } ,
25- { op : "tidy" , file : "b.txt" , fixes : [ ] }
26- ]
27- } ) ;
20+ test ( "parseBatchOperationCount counts non-empty lines" , ( ) => {
21+ const plan = [
22+ 'replace a.txt "x" "y"' ,
23+ 'doc.set b.json key "val"'
24+ ] . join ( "\n" ) ;
2825
2926 assert . equal ( parseBatchOperationCount ( plan ) , 2 ) ;
3027} ) ;
3128
32- test ( "parseBatchOperationCount returns 0 for invalid JSON " , ( ) => {
33- assert . equal ( parseBatchOperationCount ( "not json " ) , 0 ) ;
29+ test ( "parseBatchOperationCount returns 0 for empty input " , ( ) => {
30+ assert . equal ( parseBatchOperationCount ( "" ) , 0 ) ;
3431} ) ;
3532
36- test ( "parseBatchOperationCount returns 0 for missing operations " , ( ) => {
37- assert . equal ( parseBatchOperationCount ( '{"other": "data"}' ) , 0 ) ;
33+ test ( "parseBatchOperationCount returns 0 for whitespace-only input " , ( ) => {
34+ assert . equal ( parseBatchOperationCount ( " \n \n" ) , 0 ) ;
3835} ) ;
3936
40- test ( "parseBatchOperationCount returns 0 for empty operations array" , ( ) => {
41- assert . equal ( parseBatchOperationCount ( '{"operations": []}' ) , 0 ) ;
37+ test ( "parseBatchOperationCount ignores blank lines between operations" , ( ) => {
38+ const plan = 'replace a.txt "x" "y"\n\ndoc.set b.json key "v"\n' ;
39+ assert . equal ( parseBatchOperationCount ( plan ) , 2 ) ;
4240} ) ;
4341
44- test ( "parseBatchOperationCount returns 0 when operations is not an array" , ( ) => {
45- assert . equal ( parseBatchOperationCount ( '{"operations": "not-an-array"}' ) , 0 ) ;
46- assert . equal ( parseBatchOperationCount ( '{"operations": 42}' ) , 0 ) ;
47- assert . equal ( parseBatchOperationCount ( '{"operations": null}' ) , 0 ) ;
42+ test ( "parseBatchOperationCount counts a single operation" , ( ) => {
43+ assert . equal ( parseBatchOperationCount ( 'tidy.fix src/main.ts' ) , 1 ) ;
4844} ) ;
4945
5046// --- #34: snapshot-style template tests ---
5147
52- test ( "buildBatchTemplate replace operation has required fields" , ( ) => {
53- const parsed = JSON . parse ( buildBatchTemplate ( ) ) ;
54- const replace = parsed . operations [ 0 ] ;
55- assert . equal ( replace . op , "replace" ) ;
56- assert . ok ( "file" in replace , "replace operation missing 'file'" ) ;
57- assert . ok ( "from" in replace , "replace operation missing 'from'" ) ;
58- assert . ok ( "to" in replace , "replace operation missing 'to'" ) ;
48+ test ( "buildBatchTemplate replace line has file and quoted arguments" , ( ) => {
49+ const lines = buildBatchTemplate ( ) . split ( "\n" ) ;
50+ const replaceLine = lines . find ( ( l ) => l . startsWith ( "replace " ) ) ;
51+ assert . ok ( replaceLine , "template should contain a replace line" ) ;
52+ assert . match ( replaceLine , / r e p l a c e \S + " .+ " " .+ " / , "replace should have file and two quoted args" ) ;
5953} ) ;
6054
61- test ( "buildBatchTemplate tidy operation has required fields" , ( ) => {
62- const parsed = JSON . parse ( buildBatchTemplate ( ) ) ;
63- const tidy = parsed . operations [ 1 ] ;
64- assert . equal ( tidy . op , "tidy" ) ;
65- assert . ok ( "file" in tidy , "tidy operation missing 'file'" ) ;
66- assert . ok ( Array . isArray ( tidy . fixes ) , "tidy operation 'fixes' should be an array" ) ;
55+ test ( "buildBatchTemplate doc.set line has file, selector, and quoted value" , ( ) => {
56+ const lines = buildBatchTemplate ( ) . split ( "\n" ) ;
57+ const docSetLine = lines . find ( ( l ) => l . startsWith ( "doc.set " ) ) ;
58+ assert . ok ( docSetLine , "template should contain a doc.set line" ) ;
59+ assert . match ( docSetLine , / d o c \. s e t \S + \S + " .+ " / , "doc.set should have file, selector, and quoted value" ) ;
6760} ) ;
6861
69- test ( "buildBatchTemplate doc-set operation has required fields" , ( ) => {
70- const parsed = JSON . parse ( buildBatchTemplate ( ) ) ;
71- const docSet = parsed . operations [ 2 ] ;
72- assert . equal ( docSet . op , "doc-set" ) ;
73- assert . ok ( "file" in docSet , "doc-set operation missing 'file'" ) ;
74- assert . ok ( "selector" in docSet , "doc-set operation missing 'selector'" ) ;
75- assert . ok ( "value" in docSet , "doc-set operation missing 'value'" ) ;
62+ test ( "buildBatchTemplate tidy.fix line has a file path" , ( ) => {
63+ const lines = buildBatchTemplate ( ) . split ( "\n" ) ;
64+ const tidyLine = lines . find ( ( l ) => l . startsWith ( "tidy.fix " ) ) ;
65+ assert . ok ( tidyLine , "template should contain a tidy.fix line" ) ;
66+ assert . match ( tidyLine , / t i d y \. f i x \S + / , "tidy.fix should have a file path" ) ;
7667} ) ;
0 commit comments