File size: 8,315 Bytes
907b200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0dbfed
907b200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class MdSimulationE2ETest extends TestCase
{
    private string $baseUrl;
    private string $email;
    private string $password;
    private string $name;

    protected function setUp(): void
    {
        parent::setUp();

        $this->baseUrl = rtrim(
            (string) getenv('MD_E2E_BASE_URL') ?: 'http://localhost:7860/api',
            '/'
        );
        $this->email = (string) getenv('MD_E2E_EMAIL') ?: 'test@example.com';
        $this->password = (string) getenv('MD_E2E_PASSWORD') ?: 'password123';
        $this->name = (string) getenv('MD_E2E_NAME') ?: 'Test User';
    }

    // ── Health check (no auth) ────────────────────────────────

    public function test_health_check(): void
    {
        $response = Http::timeout(30)->get("{$this->baseUrl}/md-simulation/health");

        $this->assertTrue(
            $response->successful(),
            'Health check failed: ' . $response->body()
        );

        $this->assertSame('ok', $response->json('status'));
    }

    // ── Auth rejection (all protected endpoints) ──────────────

    public function test_unauthenticated_access_rejected(): void
    {
        $endpoints = [
            ['GET', '/md-simulation/history'],
            ['POST', '/md-simulation/process'],
            ['GET', '/md-simulation/status/test-123'],
            ['POST', '/md-simulation/analyze/test-123'],
            ['GET', '/md-simulation/download/test-123'],
            ['GET', '/md-simulation/download-analysis/test-123'],
        ];

        foreach ($endpoints as [$method, $uri]) {
            $response = match ($method) {
                'GET' => Http::get("{$this->baseUrl}{$uri}"),
                'POST' => Http::post("{$this->baseUrl}{$uri}", []),
            };

            $this->assertEquals(
                401,
                $response->status(),
                "Expected 401 for {$method} {$uri}, got {$response->status()}: {$response->body()}"
            );
        }
    }

    // ── Auth flow: login β†’ token β†’ authenticated request ─────

    public function test_auth_flow_and_history(): void
    {
        $token = $this->authenticate();

        $response = Http::withHeaders([
            'Authorization' => "Bearer {$token}",
            'Accept' => 'application/json',
        ])->get("{$this->baseUrl}/md-simulation/history");

        $this->assertTrue(
            $response->successful(),
            'History request failed: ' . $response->body()
        );

        $data = $response->json();
        $this->assertTrue($data['success'] ?? false);
        $this->assertArrayHasKey('results', $data['data'] ?? []);
        $this->assertArrayHasKey('pagination', $data['data'] ?? []);
    }

    // ── Validation: process without files ─────────────────────

    public function test_process_validation_no_files(): void
    {
        $token = $this->authenticate();

        $response = Http::withHeaders([
            'Authorization' => "Bearer {$token}",
            'Accept' => 'application/json',
        ])->post("{$this->baseUrl}/md-simulation/process", []);

        $this->assertEquals(
            422,
            $response->status(),
            'Expected 422, got ' . $response->status() . ': ' . $response->body()
        );
    }

    // ── Validation: wrong file type ──────────────────────────

    public function test_process_rejects_invalid_file_type(): void
    {
        $token = $this->authenticate();

        $phpFile = base_path('tests/Feature/MdSimulationE2ETest.php');
        $ligandPath = base_path('scripts/ligand.pdb');

        $this->assertFileExists($phpFile);
        $this->assertFileExists($ligandPath);

        $response = Http::withHeaders([
            'Authorization' => "Bearer {$token}",
        ])->timeout(30)->attach(
            'protein', fopen($phpFile, 'r'), 'not_a_pdb.txt'
        )->attach(
            'ligand', fopen($ligandPath, 'r'), 'ligand.pdb'
        )->post("{$this->baseUrl}/md-simulation/process", []);

        $this->assertEquals(
            422,
            $response->status(),
            'Expected 422, got ' . $response->status() . ': ' . $response->body()
        );
    }

    // ── Full submit + status flow ────────────────────────────

    public function test_full_submit_and_status_flow(): void
    {
        $token = $this->authenticate();

        $proteinPath = base_path('scripts/protein.pdb');
        $ligandPath = base_path('scripts/ligand.pdb');

        $this->assertFileExists($proteinPath);
        $this->assertFileExists($ligandPath);

        $submitResponse = Http::withHeaders([
            'Authorization' => "Bearer {$token}",
        ])->timeout(120)->attach(
            'protein', fopen($proteinPath, 'r'), 'protein.pdb'
        )->attach(
            'ligand', fopen($ligandPath, 'r'), 'ligand.pdb'
        )->post("{$this->baseUrl}/md-simulation/process", [
            'force_field' => 'ff19SB',
            'sim_time_ns' => 0.01,
            'equil_time_ns' => 0.1,
            'temperature_k' => 300,
        ]);

        $this->assertEquals(
            202,
            $submitResponse->status(),
            'Submit failed: ' . $submitResponse->body()
        );

        $submitData = $submitResponse->json();
        $this->assertTrue($submitData['success'] ?? false);
        $this->assertArrayHasKey('remote_job_id', $submitData['data'] ?? []);
        $this->assertArrayHasKey('status', $submitData['data'] ?? []);

        $remoteJobId = $submitData['data']['remote_job_id'];
        $this->assertNotNull($remoteJobId);

        // Single status check β€” proves the endpoint works without waiting
        $statusResponse = Http::withHeaders([
            'Authorization' => "Bearer {$token}",
        ])->timeout(30)->get("{$this->baseUrl}/md-simulation/status/{$remoteJobId}");

        $this->assertTrue(
            $statusResponse->successful(),
            'Status check failed: ' . $statusResponse->body()
        );

        $statusData = $statusResponse->json();
        $this->assertTrue($statusData['success'] ?? false);
        $this->assertSame(
            $remoteJobId,
            $statusData['data']['remote_job_id'] ?? null
        );
    }

    // ── Helpers ───────────────────────────────────────────────

    private function authenticate(): string
    {
        $loginResponse = Http::timeout(30)->post("{$this->baseUrl}/user/login", [
            'email' => $this->email,
            'password' => $this->password,
        ]);

        if ($loginResponse->successful()) {
            $token = $loginResponse->json('data.token');
            $this->assertNotNull($token, 'Login succeeded but no token returned');
            return $token;
        }

        // User may not exist yet β€” try registering
        $registerResponse = Http::timeout(30)->post(
            "{$this->baseUrl}/user/register",
            [
                'name' => $this->name,
                'email' => $this->email,
                'password' => $this->password,
                'password_confirmation' => $this->password,
            ]
        );

        $this->assertTrue(
            $registerResponse->successful(),
            'Cannot authenticate: login returned ' . $loginResponse->status()
            . ', register returned ' . $registerResponse->status()
            . ': ' . $registerResponse->body()
        );

        // Login after successful registration
        $loginResponse = Http::timeout(30)->post("{$this->baseUrl}/user/login", [
            'email' => $this->email,
            'password' => $this->password,
        ]);

        $this->assertTrue(
            $loginResponse->successful(),
            'Login failed after registration: ' . $loginResponse->body()
        );

        $token = $loginResponse->json('data.token');
        $this->assertNotNull($token);

        return $token;
    }
}