* ui_harness.prg - proceduri reutilizabile pentru teste UI VFP cu handshake pe semafoare.
* Un test seteaza PUBLIC gcSyncDir (folder semafoare, cu \ final) si gcUILog (fisier log),
* apoi SET PROCEDURE TO ui_harness ADDITIVE si foloseste:
*   HarnessLog(mesaj)                       - scrie o linie in log
*   HarnessStep(n, mesaj [, autoSec])       - semnaleaza pasul n gata + asteapta continuarea
*   HarnessReady(n, mesaj) / HarnessWaitContinue(n [, autoSec]) - variantele separate
*   HarnessDone([status])                   - scrie done.txt
* HarnessWaitContinue asteapta cont_<n>.txt; daca nu apare in autoSec secunde (implicit 30),
* continua singur (auto-continue) => testul merge si FARA orchestrator (doar log, fara capturi).

PROCEDURE HarnessInit
	*-- defensiv: SAFETY OFF ca STRTOFILE peste un fisier existent sa nu scoata dialogul modal
	*-- "overwrite?" (blocheaza headless); TALK OFF ca SUM/CALCULATE sa nu deseneze pe desktop.
	*-- Un test ar trebui sa faca oricum SET SAFETY OFF INAINTE de prima lui scriere de log.
	SET SAFETY OFF
	SET TALK OFF
ENDPROC

PROCEDURE HarnessLog
	LPARAMETERS tcMsg
	IF TYPE('gcUILog') <> 'C' OR EMPTY(gcUILog)
		RETURN
	ENDIF
	SET SAFETY OFF
	LOCAL lcL
	lcL = IIF(FILE(gcUILog), FILETOSTR(gcUILog), '')
	STRTOFILE(lcL + tcMsg + CHR(13)+CHR(10), gcUILog)
ENDPROC

PROCEDURE HarnessReady
	LPARAMETERS tnStep, tcNote
	SET SAFETY OFF
	STRTOFILE(TTOC(DATETIME()) + ' ' + tcNote + CHR(13)+CHR(10), gcSyncDir + 'ready_' + TRANSFORM(tnStep) + '.txt')
	HarnessLog('READY ' + TRANSFORM(tnStep) + ' - ' + tcNote)
ENDPROC

FUNCTION HarnessWaitContinue
	LPARAMETERS tnStep, tnAutoSec
	LOCAL lcCont, lnMax, lnI, llPrimit
	lcCont = gcSyncDir + 'cont_' + TRANSFORM(tnStep) + '.txt'
	lnMax  = IIF(TYPE('tnAutoSec') = 'N' AND tnAutoSec > 0, tnAutoSec, 30)
	lnI = 0
	DO WHILE !FILE(lcCont) AND lnI < (lnMax * 5)
		lnI = lnI + 1
		DOEVENTS FORCE
		= INKEY(0.2)
	ENDDO
	llPrimit = FILE(lcCont)
	HarnessLog('CONTINUE ' + TRANSFORM(tnStep) + IIF(llPrimit, ' (semnal primit)', ' (auto-continue dupa ' + TRANSFORM(lnMax) + 's)'))
	RETURN llPrimit
ENDFUNC

PROCEDURE HarnessStep
	LPARAMETERS tnStep, tcNote, tnAutoSec
	HarnessReady(tnStep, tcNote)
	HarnessWaitContinue(tnStep, tnAutoSec)
ENDPROC

PROCEDURE HarnessDone
	LPARAMETERS tcStatus
	SET SAFETY OFF
	STRTOFILE(IIF(EMPTY(tcStatus), 'done', tcStatus) + ' ' + TTOC(DATETIME()) + CHR(13)+CHR(10), gcSyncDir + 'done.txt')
	HarnessLog('GATA')
ENDPROC
