<?php

use App\Models\Document;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

describe('DocumentPolicy', function () {

    test('viewAny 允许有权限的用户查看文档列表', function () {
        $user = User::factory()->create();

        expect($user->can('viewAny', Document::class))->toBeTrue();
    });

    test('view 允许有权限的用户查看文档', function () {
        $user = User::factory()->create();
        $document = Document::factory()->create();

        expect($user->can('view', $document))->toBeTrue();
    });

    test('create 允许有权限的用户创建文档', function () {
        $user = User::factory()->create();

        expect($user->can('create', Document::class))->toBeTrue();
    });

    test('update 只允许文档上传者更新文档', function () {
        $uploader = User::factory()->create();
        $otherUser = User::factory()->create();

        $document = Document::factory()->create([
            'uploaded_by' => $uploader->id,
        ]);

        expect($uploader->can('update', $document))->toBeTrue();
        expect($otherUser->can('update', $document))->toBeFalse();
    });

    test('delete 只允许文档上传者删除文档', function () {
        $uploader = User::factory()->create();
        $otherUser = User::factory()->create();

        $document = Document::factory()->create([
            'uploaded_by' => $uploader->id,
        ]);

        expect($uploader->can('delete', $document))->toBeTrue();
        expect($otherUser->can('delete', $document))->toBeFalse();
    });

    test('download 允许有权限的用户下载文档', function () {
        $user = User::factory()->create();
        $document = Document::factory()->create();

        expect($user->can('download', $document))->toBeTrue();
    });
});
