1- using System . IO . Compression ;
2- using System . Net ;
31using System . Net . Http . Headers ;
42using System . Net . Http . Json ;
53using System . Text ;
64using System . Text . Json . Nodes ;
5+ using PythonScriptRunner . Tests . Infrastructure ;
76
87namespace PythonScriptRunner . AppHost . Tests ;
98
@@ -13,8 +12,6 @@ namespace PythonScriptRunner.AppHost.Tests;
1312/// </summary>
1413public sealed class AppHostTests
1514{
16- private const string DevPassword = "psr-dev" ;
17-
1815 private static readonly TimeSpan StartTimeout = TimeSpan . FromMinutes ( 5 ) ;
1916
2017 private static CancellationToken Ct => TestContext . Current . CancellationToken ;
@@ -55,28 +52,23 @@ await Task.WhenAll(
5552 }
5653
5754 // AC-2: the fixed user autor imports the fixtures, publishes them and runs CS-1 and CS-2.
58- var dashboard = app . GetEndpoint ( "dashboard" , "http" ) ;
59- using var http = new HttpClient ( new HttpClientHandler { CookieContainer = new CookieContainer ( ) } ) { BaseAddress = dashboard } ;
60- http . DefaultRequestHeaders . Add ( "X-PSR-Request" , "1" ) ;
61- ( await http . PostAsJsonAsync ( "/psr/api/auth/login" , new { name = "autor" , password = DevPassword } , Ct ) ) . StatusCode
62- . ShouldBe ( HttpStatusCode . NoContent ) ;
63-
55+ using var http = await PanelApi . LoginAsync ( app . GetEndpoint ( "dashboard" , "http" ) ) ;
6456 var smtp = app . GetEndpoint ( "mailpit" , "smtp" ) ;
65- await ImportAndPublishAsync ( http , "calculo-progressao" , static ( _ , content ) => content ) ;
57+ await PanelApi . ImportAndPublishAsync ( http , "calculo-progressao" ) ;
6658 var runsCs2 = ! OperatingSystem . IsWindows ( ) ;
6759 if ( runsCs2 )
6860 {
69- await ImportAndPublishAsync ( http , "html-para-email" , ( path , content ) => path . EndsWith ( ".pipeline.json" , StringComparison . Ordinal )
61+ await PanelApi . ImportAndPublishAsync ( http , "html-para-email" , ( path , content ) => path . EndsWith ( ".pipeline.json" , StringComparison . Ordinal )
7062 ? content . Replace ( "smtp.exemplo.com" , smtp . Host , StringComparison . Ordinal )
7163 . Replace ( "\" const\" : 465" , $ "\" const\" : { smtp . Port } ", StringComparison . Ordinal )
7264 : content ) ;
7365 }
7466
75- var cs1 = await RunAsync ( http , "calculo-progressao" , JsonContent . Create ( new
67+ var cs1 = await PanelApi . RunAsync ( http , "calculo-progressao" , JsonContent . Create ( new
7668 {
7769 inputs = new { a1 = 1 , razao = 2 , n = 3 , coeficientes = new [ ] { 0 , 1 } } ,
7870 } ) ) ;
79- ( await WaitForRunAsync ( http , cs1 ) ) [ "outputs" ] ! [ "soma" ] ! . GetValue < double > ( ) . ShouldBe ( 9 ) ;
71+ ( await PanelApi . WaitForSuccessAsync ( http , cs1 ) ) [ "outputs" ] ! [ "soma" ] ! . GetValue < double > ( ) . ShouldBe ( 9 ) ;
8072
8173 // CS-2 needs weasyprint (GTK/pango), which runs on Linux only (D-106).
8274 if ( runsCs2 )
@@ -86,86 +78,11 @@ await ImportAndPublishAsync(http, "html-para-email", (path, content) => path.End
8678 var html = new ByteArrayContent ( Encoding . UTF8 . GetBytes ( "<html><body><h1>Relatório</h1></body></html>" ) ) ;
8779 html . Headers . ContentType = new MediaTypeHeaderValue ( "text/html" ) ;
8880 form . Add ( html , "html" , "pagina.html" ) ;
89- var cs2 = await RunAsync ( http , "html-para-email" , form ) ;
90- await WaitForRunAsync ( http , cs2 ) ;
81+ await PanelApi . WaitForSuccessAsync ( http , await PanelApi . RunAsync ( http , "html-para-email" , form ) ) ;
9182
9283 using var mailpit = app . CreateHttpClient ( "mailpit" , "http" ) ;
9384 var messages = ( await mailpit . GetFromJsonAsync < JsonObject > ( "/api/v1/messages" , Ct ) ) ! [ "messages" ] ! . AsArray ( ) ;
9485 messages . Select ( m => m ! [ "Subject" ] ! . GetValue < string > ( ) ) . ShouldContain ( "Relatório do Aspire" ) ;
9586 }
9687 }
97-
98- /// <summary>Imports a fixture as a zip (drafts), then publishes its stages and pipelines with their ETags.</summary>
99- private static async Task ImportAndPublishAsync ( HttpClient http , string fixture , Func < string , string , string > transform )
100- {
101- var root = Path . Combine ( RepositoryRoot ( ) , "tests" , "fixtures" , fixture ) ;
102- using var zip = new MemoryStream ( ) ;
103- using ( var archive = new ZipArchive ( zip , ZipArchiveMode . Create , leaveOpen : true ) )
104- {
105- foreach ( var file in Directory . EnumerateFiles ( root , "*" , SearchOption . AllDirectories ) . Where ( f => ! f . Contains ( "__pycache__" , StringComparison . Ordinal ) ) )
106- {
107- var relative = Path . GetRelativePath ( root , file ) . Replace ( '\\ ' , '/' ) ;
108- await using var writer = new StreamWriter ( archive . CreateEntry ( relative ) . Open ( ) ) ;
109- await writer . WriteAsync ( transform ( relative , await File . ReadAllTextAsync ( file , Ct ) ) ) ;
110- }
111- }
112-
113- using var form = new MultipartFormDataContent ( ) ;
114- var archiveContent = new ByteArrayContent ( zip . ToArray ( ) ) ;
115- archiveContent . Headers . ContentType = new MediaTypeHeaderValue ( "application/zip" ) ;
116- form . Add ( archiveContent , "archive" , $ "{ fixture } .zip") ;
117- var import = await http . PostAsync ( "/psr/api/import" , form , Ct ) ;
118- import . StatusCode . ShouldBe ( HttpStatusCode . OK , await import . Content . ReadAsStringAsync ( Ct ) ) ;
119- var items = ( await import . Content . ReadFromJsonAsync < JsonObject > ( Ct ) ) ! [ "items" ] ! . AsArray ( ) ;
120-
121- // Stages first: the pipeline references their published versions.
122- foreach ( var item in items . OrderBy ( i => i ! [ "kind" ] ! . GetValue < string > ( ) == "Stage" ? 0 : 1 ) )
123- {
124- var kind = item ! [ "kind" ] ! . GetValue < string > ( ) == "Stage" ? "stages" : "pipelines" ;
125- var key = item [ "key" ] ! . GetValue < string > ( ) ;
126- var draft = await http . GetAsync ( $ "/psr/api/{ kind } /{ key } /draft", Ct ) ;
127- using var publish = new HttpRequestMessage ( HttpMethod . Post , $ "/psr/api/{ kind } /{ key } /draft/publish") ;
128- publish . Headers . IfMatch . Add ( draft . Headers . ETag ! ) ;
129- var published = await http . SendAsync ( publish , Ct ) ;
130- published . StatusCode . ShouldBe ( HttpStatusCode . OK , await published . Content . ReadAsStringAsync ( Ct ) ) ;
131- }
132- }
133-
134- private static async Task < string > RunAsync ( HttpClient http , string pipeline , HttpContent body )
135- {
136- var response = await http . PostAsync ( $ "/psr/api/pipelines/{ pipeline } /runs", body , Ct ) ;
137- response . IsSuccessStatusCode . ShouldBeTrue ( await response . Content . ReadAsStringAsync ( Ct ) ) ;
138- return ( await response . Content . ReadFromJsonAsync < JsonObject > ( Ct ) ) ! [ "runId" ] ! . GetValue < string > ( ) ;
139- }
140-
141- private static async Task < JsonObject > WaitForRunAsync ( HttpClient http , string runId )
142- {
143- using var timeout = CancellationTokenSource . CreateLinkedTokenSource ( Ct ) ;
144- timeout . CancelAfter ( TimeSpan . FromMinutes ( 5 ) ) ;
145- while ( true )
146- {
147- var run = ( await http . GetFromJsonAsync < JsonObject > ( $ "/psr/api/runs/{ runId } ", timeout . Token ) ) ! ;
148- var status = run [ "status" ] ! . GetValue < string > ( ) ;
149- if ( status is "Succeeded" or "Failed" or "Cancelled" or "TimedOut" )
150- {
151- status . ShouldBe ( "Succeeded" , run [ "error" ] ? . ToJsonString ( ) ) ;
152- return run ;
153- }
154-
155- await Task . Delay ( TimeSpan . FromSeconds ( 1 ) , timeout . Token ) ;
156- }
157- }
158-
159- private static string RepositoryRoot ( )
160- {
161- for ( var directory = new DirectoryInfo ( AppContext . BaseDirectory ) ; directory is not null ; directory = directory . Parent )
162- {
163- if ( File . Exists ( Path . Combine ( directory . FullName , "PythonScriptRunner.slnx" ) ) )
164- {
165- return directory . FullName ;
166- }
167- }
168-
169- throw new InvalidOperationException ( "The repository root (PythonScriptRunner.slnx) was not found." ) ;
170- }
17188}
0 commit comments