File size: 12,589 Bytes
e6b7e49
 
288c05b
 
 
e6b7e49
 
f7d6ffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6b7e49
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
288c05b
e6b7e49
288c05b
 
 
 
 
 
 
 
 
f7d6ffd
 
 
 
e6b7e49
 
 
f7d6ffd
 
 
e6b7e49
 
 
 
 
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
288c05b
e6b7e49
 
 
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
 
 
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
 
 
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
 
 
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
288c05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d6ffd
 
 
288c05b
 
 
 
 
e6b7e49
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php

use Illuminate\Http\Client\Request as HttpRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
use Inertia\Testing\AssertableInertia as Assert;

function lecturerAuthUserCookie(): string
{
    return json_encode([
        'email' => 'lecturer@example.com',
        'id' => 10,
        'identity_number' => 'D12345',
        'name' => 'Dosen',
        'role' => 'lecturer',
        'status' => 'active',
    ], JSON_THROW_ON_ERROR);
}

test('dosen routes redirect guests to login', function () {
    $this->get(route('dosen'))
        ->assertRedirect(route('login'));
});

test('dosen dashboard page can be rendered', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/courses/101/documents' => Http::response([
            'data' => [
                [
                    'course_id' => '101',
                    'created_at' => '2026-05-01T10:00:00Z',
                    'file_size' => 1200,
                    'file_type' => 'pdf',
                    'filename' => 'materi.pdf',
                    'id' => 'doc-1',
                    'status' => 'ready',
                    'updated_at' => '2026-05-02T10:00:00Z',
                    'uploaded_by' => 'dosen-1',
                ],
            ],
        ]),
        'https://rag.test/api/v1/courses*' => Http::response([
            'data' => [
                [
                    'description' => 'Materi dasar algoritma',
                    'id' => 101,
                    'title' => 'Algoritma & Pemrograman',
                ],
            ],
            'limit' => 100,
            'page' => 1,
            'total' => 1,
        ]),
    ]);

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->get(route('dosen'))
        ->assertOk()
        ->assertInertia(fn (Assert $page) => $page
            ->component('dosen')
            ->where('backendMessage', null)
            ->has('knowledgeBases', 1)
            ->where('knowledgeBases.0.id', '101')
            ->where('knowledgeBases.0.name', 'Algoritma & Pemrograman')
            ->where('knowledgeBases.0.documentCount', 1)
            ->where('knowledgeBases.0.failedDocumentCount', 0)
            ->where('knowledgeBases.0.updatedAt', '2026-05-02'));

    Http::assertSent(fn (HttpRequest $request): bool => $request->method() === 'GET'
        && str_starts_with($request->url(), 'https://rag.test/api/v1/courses?')
        && $request->hasHeader('Authorization', 'Bearer token'));
});

test('dosen settings page can be rendered', function () {
    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->get(route('dosen.pengaturan'))
        ->assertOk()
        ->assertInertia(fn (Assert $page) => $page->component('dosen/pengaturan'));
});

test('dosen course detail page can be rendered', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/courses/101/documents' => Http::response([
            'data' => [
                [
                    'course_id' => '101',
                    'created_at' => '2026-05-01T10:00:00Z',
                    'file_size' => 1200,
                    'file_type' => 'pdf',
                    'filename' => 'materi.pdf',
                    'id' => 'doc-1',
                    'status' => 'ready',
                    'updated_at' => '2026-05-02T10:00:00Z',
                    'uploaded_by' => 'dosen-1',
                ],
            ],
        ]),
        'https://rag.test/api/v1/courses/101' => Http::response([
            'description' => 'Materi dasar algoritma',
            'id' => 101,
            'title' => 'Algoritma & Pemrograman',
        ]),
    ]);

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->get(route('dosen.show', ['courseId' => '101']))
        ->assertOk()
        ->assertInertia(fn (Assert $page) => $page
            ->component('dosen-detail')
            ->where('backendMessage', null)
            ->where('courseId', '101')
            ->where('course.id', 101)
            ->where('course.title', 'Algoritma & Pemrograman')
            ->has('documents', 1)
            ->where('documents.0.id', 'doc-1')
            ->where('documents.0.filename', 'materi.pdf'));

    Http::assertSent(fn (HttpRequest $request): bool => $request->method() === 'GET'
        && $request->url() === 'https://rag.test/api/v1/courses/101'
        && $request->hasHeader('Authorization', 'Bearer token'));

    Http::assertSent(fn (HttpRequest $request): bool => $request->method() === 'GET'
        && $request->url() === 'https://rag.test/api/v1/courses/101/documents'
        && $request->hasHeader('Authorization', 'Bearer token'));
});

test('dosen course can be created through laravel proxy', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/courses' => Http::response([
            'description' => 'Materi dasar algoritma',
            'id' => 101,
            'title' => 'Algoritma & Pemrograman',
        ], 201),
    ]);

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->from(route('dosen'))
        ->post(route('dosen.courses.store'), [
            'description' => 'Materi dasar algoritma',
            'title' => 'Algoritma & Pemrograman',
        ])
        ->assertRedirect(route('dosen'))
        ->assertSessionHasNoErrors();

    Http::assertSent(fn (HttpRequest $request): bool => $request->method() === 'POST'
        && $request->url() === 'https://rag.test/api/v1/courses'
        && $request->hasHeader('Authorization', 'Bearer token')
        && $request['title'] === 'Algoritma & Pemrograman'
        && $request['description'] === 'Materi dasar algoritma');
});

test('dosen course create uses backend error message', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/courses' => Http::response([
            'message' => 'Course already exists.',
        ], 422),
    ]);

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->from(route('dosen'))
        ->post(route('dosen.courses.store'), [
            'title' => 'Algoritma & Pemrograman',
        ])
        ->assertRedirect(route('dosen'))
        ->assertSessionHasErrors(['course' => 'Course already exists.']);
});

test('dosen course can be updated through laravel proxy', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/courses/101' => Http::response([
            'description' => 'Materi semester awal',
            'id' => 101,
            'title' => 'Algoritma Lanjut',
        ]),
    ]);

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->from(route('dosen.show', ['courseId' => '101']))
        ->put(route('dosen.update', ['courseId' => '101']), [
            'description' => 'Materi semester awal',
            'title' => 'Algoritma Lanjut',
        ])
        ->assertRedirect(route('dosen.show', ['courseId' => '101']))
        ->assertSessionHasNoErrors();

    Http::assertSent(fn (HttpRequest $request): bool => $request->method() === 'PUT'
        && $request->url() === 'https://rag.test/api/v1/courses/101'
        && $request->hasHeader('Authorization', 'Bearer token')
        && $request['title'] === 'Algoritma Lanjut'
        && $request['description'] === 'Materi semester awal');
});

test('dosen course update uses backend error message', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/courses/101' => Http::response([
            'message' => 'Course title already exists.',
        ], 422),
    ]);

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->from(route('dosen.show', ['courseId' => '101']))
        ->put(route('dosen.update', ['courseId' => '101']), [
            'title' => 'Algoritma Lanjut',
        ])
        ->assertRedirect(route('dosen.show', ['courseId' => '101']))
        ->assertSessionHasErrors(['course' => 'Course title already exists.']);
});

test('dosen course can be deleted through laravel proxy', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/courses/101' => Http::response([]),
    ]);

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->from(route('dosen.show', ['courseId' => '101']))
        ->delete(route('dosen.destroy', ['courseId' => '101']))
        ->assertRedirect(route('dosen'))
        ->assertSessionHasNoErrors();

    Http::assertSent(fn (HttpRequest $request): bool => $request->method() === 'DELETE'
        && $request->url() === 'https://rag.test/api/v1/courses/101'
        && $request->hasHeader('Authorization', 'Bearer token'));
});

test('dosen course delete uses backend error message', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/courses/101' => Http::response([
            'message' => 'Course still has active documents.',
        ], 422),
    ]);

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->from(route('dosen.show', ['courseId' => '101']))
        ->delete(route('dosen.destroy', ['courseId' => '101']))
        ->assertRedirect(route('dosen.show', ['courseId' => '101']))
        ->assertSessionHasErrors(['course' => 'Course still has active documents.']);
});

test('dosen document can be uploaded through laravel proxy', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/documents/upload' => Http::response([
            'document_id' => 'doc-1',
            'filename' => 'materi.pdf',
            'message' => 'File accepted.',
            'status' => 'processing',
        ], 202),
    ]);

    $file = UploadedFile::fake()->create('materi.pdf', 100, 'application/pdf');

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->from(route('dosen.show', ['courseId' => '101']))
        ->post(route('dosen.documents.store', ['courseId' => '101']), [
            'file' => $file,
        ])
        ->assertRedirect(route('dosen.show', ['courseId' => '101']))
        ->assertSessionHasNoErrors();

    Http::assertSent(fn (HttpRequest $request): bool => $request->method() === 'POST'
        && $request->url() === 'https://rag.test/api/v1/documents/upload'
        && $request->hasHeader('Authorization', 'Bearer token')
        && $request->isMultipart()
        && $request->hasFile('file', filename: 'materi.pdf')
        && collect($request->data())->contains(
            fn (array $part): bool => ($part['name'] ?? null) === 'course_id'
                && ($part['contents'] ?? null) === '101',
        ));
});

test('dosen document upload uses backend error message', function () {
    config(['services.rag.base_url' => 'https://rag.test/api/v1']);

    Http::fake([
        'https://rag.test/api/v1/documents/upload' => Http::response([
            'detail' => [
                ['msg' => 'Unsupported file type.'],
            ],
        ], 422),
    ]);

    $file = UploadedFile::fake()->create('materi.pdf', 100, 'application/pdf');

    $this->withCookie('sevima_raghub_auth_token', 'token')
        ->withCookie('sevima_raghub_auth_user', lecturerAuthUserCookie())
        ->from(route('dosen.show', ['courseId' => '101']))
        ->post(route('dosen.documents.store', ['courseId' => '101']), [
            'file' => $file,
        ])
        ->assertRedirect(route('dosen.show', ['courseId' => '101']))
        ->assertSessionHasErrors(['file' => 'Unsupported file type.']);
});