<?php
namespace Site\Test;

require_once __DIR__.'/../../../autoload.php';

use Site\Lib\Tester;
use Site\Lib\Activitypub;
use Site\Controller\BlogPost;

if (ACTIVITYPUB_ENABLED) {
  $test = new Tester([
    'colorOutput' => true,
    'verboseOutput' => true
  ]);

  /*
  postInbox(array $activity): void
  update(): string
  sendActorUpdate(array $params): void
  */

  $test->describe('ActivityPub情報の受け取り', function($test): void {
    $test->it('このインスタンスのユーザーを受け取れるはず', function($test): void {
      $ap = new Activitypub();
      $res = $ap->getActor();
      $test->assertNotNull($res);
      $json = json_decode($res, true);

      $expect = 'Person';
      $test->assertEquals($json['type'], $expect);

      $expect = 'http://127.0.0.1:8000';
      $test->assertEquals($json['url'], $expect);

      $expect = 'http://127.0.0.1:8000/ap/actor';
      $test->assertEquals($json['id'], $expect);

      $expect = 'http://127.0.0.1:8000/ap/inbox';
      $test->assertEquals($json['inbox'], $expect);

      $expect = 'http://127.0.0.1:8000/ap/outbox';
      $test->assertEquals($json['outbox'], $expect);

      $expect = 'http://127.0.0.1:8000/ap/followers';
      $test->assertEquals($json['followers'], $expect);

      $expect = 'http://127.0.0.1:8000/ap/following';
      $test->assertEquals($json['following'], $expect);
    });
    
    $test->it('アクティビティを受け取れるはず', function($test): void {
      $blog = new BlogPost;
      $posts = $blog->getPosts('/blog/', null);
      $ap = new Activitypub($posts);
      $res = $ap->getActivity('a8c04518-4181-4ec6-9ef0-3f88f23b84b6'); // /blog/feature-test
      $test->assertNotNull($res);
      $json = json_decode($res, true);

      $expect = 'Create';
      $test->assertEquals($json['type'], $expect);

      $expect = '【マークダウン】関数';
      $test->assertEquals($json['object']['name'], $expect);

      $expect = 'https://www.w3.org/ns/activitystreams#Public';
      $test->assertEquals($json['object']['to'][0], $expect);

      $expect = 'zBbw7K7L1dEwr6htZX5JevRv3oLtHwXQU4o5jT8jn9RKF';
      $test->assertEquals($json['attachment'][0]['digestMultibase'], $expect);

      $expect = 'http://127.0.0.1:8000/static/article/o_53803618dc1691.28179609-orig.jpg';
      $test->assertEquals($json['attachment'][0]['url'], $expect);
    });
  });

  // TODO: 未完了

  $test->printSummary();
}