@@ -12,12 +12,15 @@ import * as Layer from "effect/Layer";
1212import * as Option from "effect/Option" ;
1313import * as Path from "effect/Path" ;
1414import * as PlatformError from "effect/PlatformError" ;
15+ import { ChildProcessSpawner } from "effect/process" ;
16+ import * as Metric from "effect/Metric" ;
1517import { assert , it } from "@effect/vitest" ;
1618
1719import { CheckpointRef , GitCommandError , VcsProcessExitError } from "@t3tools/contracts" ;
1820import * as HostProcess from "@t3tools/shared/HostProcess" ;
1921import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
2022import * as ServerConfig from "../config.ts" ;
23+ import { gitCommandDuration , gitCommandsTotal } from "../observability/Metrics.ts" ;
2124import * as CheckpointStore from "../checkpointing/CheckpointStore.ts" ;
2225import * as ProcessRunner from "../processRunner.ts" ;
2326import * as GitVcsDriver from "./GitVcsDriver.ts" ;
@@ -1017,3 +1020,124 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
10171020 assert . strictEqual ( detectionError . _tag , "VcsProcessSpawnError" ) ;
10181021 } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
10191022) ;
1023+
1024+ const gitCommandCount = ( operation : string , outcome : string ) =>
1025+ Metric . value (
1026+ Metric . withAttributes ( gitCommandsTotal , [
1027+ [ "operation" , operation ] ,
1028+ [ "outcome" , outcome ] ,
1029+ ] ) ,
1030+ ) . pipe ( Effect . map ( ( state ) => state . count ) ) ;
1031+
1032+ const gitCommandDurationCount = ( operation : string ) =>
1033+ Metric . value ( Metric . withAttributes ( gitCommandDuration , [ [ "operation" , operation ] ] ) ) . pipe (
1034+ Effect . map ( ( state ) => state . count ) ,
1035+ ) ;
1036+
1037+ const makeMeteredDriver = ( exitCode : number , failure ?: VcsProcessExitError ) =>
1038+ GitVcsDriver . makeVcsDriverShape ( ) . pipe (
1039+ Effect . provideService ( VcsProcess . VcsProcess , {
1040+ run : ( ) =>
1041+ failure === undefined
1042+ ? Effect . succeed ( {
1043+ exitCode : ChildProcessSpawner . ExitCode ( exitCode ) ,
1044+ stdout : "" ,
1045+ stderr : "" ,
1046+ stdoutTruncated : false ,
1047+ stderrTruncated : false ,
1048+ } )
1049+ : Effect . fail ( failure ) ,
1050+ } ) ,
1051+ ) ;
1052+
1053+ it . effect ( "GitVcsDriver meters each git command once under its operation" , ( ) =>
1054+ Effect . gen ( function * ( ) {
1055+ const operation = "GitVcsDriver.checkpoints.test.metered" ;
1056+ const driver = yield * makeMeteredDriver ( 0 ) ;
1057+
1058+ yield * driver . execute ( { operation, cwd : "/repo" , args : [ "status" ] } ) ;
1059+ yield * driver . execute ( { operation, cwd : "/repo" , args : [ "status" ] } ) ;
1060+
1061+ assert . strictEqual ( yield * gitCommandCount ( operation , "success" ) , 2 ) ;
1062+ assert . strictEqual ( yield * gitCommandCount ( operation , "failure" ) , 0 ) ;
1063+ assert . strictEqual ( yield * gitCommandDurationCount ( operation ) , 2 ) ;
1064+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
1065+ ) ;
1066+
1067+ it . effect ( "GitVcsDriver counts an allowed non-zero exit as a success" , ( ) =>
1068+ Effect . gen ( function * ( ) {
1069+ const operation = "GitVcsDriver.checkpoints.test.allowedNonZero" ;
1070+ const driver = yield * makeMeteredDriver ( 1 ) ;
1071+
1072+ const result = yield * driver . execute ( {
1073+ operation,
1074+ cwd : "/repo" ,
1075+ args : [ "diff" , "--quiet" ] ,
1076+ allowNonZeroExit : true ,
1077+ } ) ;
1078+
1079+ assert . strictEqual ( result . exitCode , 1 ) ;
1080+ assert . strictEqual ( yield * gitCommandCount ( operation , "success" ) , 1 ) ;
1081+ assert . strictEqual ( yield * gitCommandCount ( operation , "failure" ) , 0 ) ;
1082+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
1083+ ) ;
1084+
1085+ it . effect ( "GitVcsDriver counts a failed git command as a failure" , ( ) =>
1086+ Effect . gen ( function * ( ) {
1087+ const operation = "GitVcsDriver.checkpoints.test.failed" ;
1088+ const driver = yield * makeMeteredDriver (
1089+ 128 ,
1090+ new VcsProcessExitError ( {
1091+ operation,
1092+ command : "git" ,
1093+ cwd : "/repo" ,
1094+ exitCode : 128 ,
1095+ detail : "fatal" ,
1096+ } ) ,
1097+ ) ;
1098+
1099+ const error = yield * driver
1100+ . execute ( { operation, cwd : "/repo" , args : [ "rev-parse" , "HEAD" ] } )
1101+ . pipe ( Effect . flip ) ;
1102+
1103+ assert . strictEqual ( error . _tag , "VcsProcessExitError" ) ;
1104+ assert . strictEqual ( yield * gitCommandCount ( operation , "failure" ) , 1 ) ;
1105+ assert . strictEqual ( yield * gitCommandCount ( operation , "success" ) , 0 ) ;
1106+ assert . strictEqual ( yield * gitCommandDurationCount ( operation ) , 1 ) ;
1107+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
1108+ ) ;
1109+
1110+ it . effect ( "checkpoint capture meters every git process it starts, including streamed ones" , ( ) =>
1111+ Effect . gen ( function * ( ) {
1112+ const fileSystem = yield * FileSystem . FileSystem ;
1113+ const liveProcess = yield * VcsProcess . VcsProcess ;
1114+ const operation = VcsProcess . CHECKPOINT_CAPTURE_OPERATION ;
1115+ let started = 0 ;
1116+ let streamed = 0 ;
1117+ const driver = yield * GitVcsDriver . makeVcsDriverShape ( ) ;
1118+ const captureDriver = yield * GitVcsDriver . makeVcsDriverShape ( ) . pipe (
1119+ Effect . provideService ( VcsProcess . VcsProcess , {
1120+ run : ( input ) => {
1121+ if ( input . operation === operation ) {
1122+ started ++ ;
1123+ if ( input . onStdoutChunk !== undefined ) streamed ++ ;
1124+ }
1125+ return liveProcess . run ( input ) ;
1126+ } ,
1127+ } ) ,
1128+ ) ;
1129+ const cwd = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-checkpoint-metrics-" } ) ;
1130+ const { checkpointRef } = yield * makeCheckpointFixture ( driver , cwd ) ;
1131+ const before =
1132+ ( yield * gitCommandCount ( operation , "success" ) ) +
1133+ ( yield * gitCommandCount ( operation , "failure" ) ) ;
1134+
1135+ yield * captureDriver . checkpoints . captureCheckpoint ( { cwd, checkpointRef } ) ;
1136+
1137+ const after =
1138+ ( yield * gitCommandCount ( operation , "success" ) ) +
1139+ ( yield * gitCommandCount ( operation , "failure" ) ) ;
1140+ assert . isTrue ( streamed > 0 ) ;
1141+ assert . strictEqual ( after - before , started ) ;
1142+ } ) . pipe ( Effect . scoped , Effect . provide ( layerGitContract ) ) ,
1143+ ) ;
0 commit comments