unit TestuntAPI;
{

  Delphi DUnit Test Case
  ----------------------
  This unit contains a skeleton test case class generated by the Test Case Wizard.
  Modify the generated code to correctly setup and call the methods from the unit 
  being tested.

}

interface

uses
  TestFramework, JSON.Serializers, Classes, untAPI, System.Net.HttpClient,
  System.Net.URLClient, System.Net.HttpClientComponent, SysUtils;

type
  // Test methods for class TMCSignInReq

  TestTMCSignInReq = class(TTestCase)
  strict private
    FMCSignInReq: TMCSignInReq;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestToJson;
    procedure TestFromJson;
  end;
  // Test methods for class TAPI

  TestTAPI = class(TTestCase)
  strict private
    FAPI: TAPI;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestMCLogin;
    procedure TestMCValidate;
    procedure TestSavePuzzle;
  end;

implementation

uses
  untJSON;

procedure TestTMCSignInReq.SetUp;
begin
  FMCSignInReq := TMCSignInReq.Create('rarnu', 'Rarnu1120');
end;

procedure TestTMCSignInReq.TearDown;
begin
  FMCSignInReq.Free;
  FMCSignInReq := nil;
end;

procedure TestTMCSignInReq.TestFromJson;
begin

end;

procedure TestTMCSignInReq.TestToJson;
var
  JsonValue: string;
begin
  JsonValue := TJSON.ObjectToJson<TMCSignInReq>(FMCSignInReq);
  Writeln(JsonValue);
end;

procedure TestTAPI.SetUp;
begin
  FAPI := TAPI.Create;
end;

procedure TestTAPI.TearDown;
begin
  FAPI.Free;
  FAPI := nil;
end;

procedure TestTAPI.TestMCLogin;
var
  ReturnValue: TMCUserWithToken;
  APassword: string;
  AAccount: string;
begin
  ReturnValue := TAPI.MCLogin('rarnu', 'Rarnu1120');
  // TODO: Validate method results
  if Assigned(ReturnValue) then begin
    Writeln(TJSON.ObjectToJson<TMCUserWithToken>(ReturnValue));
    // Assert(ReturnValue = '222');
    ReturnValue.Free;
  end;

end;

procedure TestTAPI.TestMCValidate;
var
  ReturnValue: TMCUser;
  AToken: string;
begin
  AToken := 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NDE4NjgsImlhdCI6MTcyOTQ3OTkyMiwiZXhwIjoxNzYxMDE1OTIyfQ.aPiaflh1dwgcL4sbY31w8q70X9ajnQfF4i1ZRKcfS8M';
  ReturnValue := TAPI.MCValidate(AToken);
  if Assigned(ReturnValue) then begin
    Writeln(TJSON.ObjectToJson<TMCUser>(ReturnValue));
    ReturnValue.Free;
  end;
end;

procedure TestTAPI.TestSavePuzzle;
var
   APuzzle: TEntityPuzzle;
   ReturnValue: TSavePuzzleResult;
   AUserId: Int64;
   AToken: string;
begin
  AUserId := 41868;
  AToken := 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NDE4NjgsImlhdCI6MTcyOTQ3OTkyMiwiZXhwIjoxNzYxMDE1OTIyfQ.aPiaflh1dwgcL4sbY31w8q70X9ajnQfF4i1ZRKcfS8M';
  APuzzle := TEntityPuzzle.Create;
  APuzzle.Id := 3;
  APuzzle.Name := 'test2333';
  APuzzle.UserId := AUserId;
  APuzzle.Contributor := 'rarnu';
  APuzzle.Message := '23333';
  APuzzle.Solution := 'ÎÞ½âµÄ';
  APuzzle.CoverCard := 10000;
  APuzzle.LuaScript := 'aux.BeginPuzzle()';
  ReturnValue := TAPI.SavePuzzle(APuzzle, AUserId, AToken);
  if Assigned(ReturnValue) then begin
    Writeln(TJSON.ObjectToJson<TSavePuzzleResult>(ReturnValue));
    ReturnValue.Free;
  end;

  APuzzle.Free;
end;

initialization
  // Register any test cases with the test runner
  // RegisterTest(TestTMCSignInReq.Suite);
  // RegisterTest(TestTAPI.Suite);
end.

