% SPDX-FileCopyrightText: 2023 Henry Bubert
%
% SPDX-License-Identifier: LGPL-2.1-or-later
-module(cable_posts_SUITE).
-include_lib("eunit/include/eunit.hrl").
%% move these to seperate test file
post_text_test() ->
[{binary, Bin}, {obj, TestObj}] = examples:post_text(),
Decoded = posts:decode_post_text(Bin),
[ {pubKey, _PubKey}
, {links, _Links}
, {postType, _PostType}
, {timestamp, _TimeStamp}
, {channel, Channel}
, {text, Text}
] = Decoded,
?assertEqual("default", Channel),
?assertEqual(<<"h€llo world"/utf8>>, Text),
assert_post(Decoded, TestObj).
post_delete_test() ->
[{binary, Bin}, {obj, TestObj}] = examples:post_delete(),
Decoded = posts:decode_post_delete(Bin),
[ {pubKey, _PubKey}
, {links, _Links}
, {postType, _PostType}
, {timestamp, _TimeStamp}
, {hashes, Hashes}
] = Decoded,
% TODO: assert Hash values
?assertEqual(length(maps:get(<<"hashes">>, TestObj)), length(Hashes)),
assert_post(Decoded, TestObj).
post_info_test() ->
[{binary, Bin}, {obj, TestObj}] = examples:post_info(),
Decoded = posts:decode_post_info(Bin),
[ {pubKey, _PubKey}
, {links, _Links}
, {postType, _PostType}
, {timestamp, _TimeStamp}
, {infos, Infos}
] = Decoded,
?assertEqual(#{"name" => "cabler"}, Infos),
assert_post(Decoded, TestObj).
post_topic_test() ->
[{binary, Bin}, {obj, TestObj}] = examples:post_topic(),
Decoded = posts:decode_post_topic(Bin),
[ {pubKey, _PubKey}
, {links, _Links}
, {postType, _PostType}
, {timestamp, _TimeStamp}
, {channel, Channel}
, {topic, Topic}
] = Decoded,
?assertEqual(maps:get(<<"channel">>, TestObj), Channel),
?assertEqual(maps:get(<<"topic">>, TestObj), Topic),
assert_post(Decoded, TestObj).
post_join_test() ->
[{binary, Bin}, {obj, TestObj}] = examples:post_join(),
Decoded = posts:decode_post_join(Bin),
[ {pubKey, _PubKey}
, {links, _Links}
, {postType, _PostType}
, {timestamp, _TimeStamp}
, {channel, Channel}
] = Decoded,
?assertEqual(maps:get(<<"channel">>, TestObj), Channel),
assert_post(Decoded, TestObj).
post_leave_test() ->
[{binary, Bin}, {obj, TestObj}] = examples:post_leave(),
Decoded = posts:decode_post_leave(Bin),
[ {pubKey, _PubKey}
, {links, _Links}
, {postType, _PostType}
, {timestamp, _TimeStamp}
, {channel, Channel}
] = Decoded,
?assertEqual(maps:get(<<"channel">>, TestObj), Channel),
assert_post(Decoded, TestObj).
% Helpers
%%%%%%%%%
assert_post(Post, TestObj) ->
?assertEqual(maps:get(<<"postType">>, TestObj), proplists:get_value(postType, Post)),
?assertEqual(maps:get(<<"timestamp">>, TestObj), proplists:get_value(timestamp, Post)),
?assertEqual(length(maps:get(<<"links">>, TestObj)), length(proplists:get_value(links, Post))),
PubKey = hex:hexstr_to_bin(binary_to_list(maps:get(<<"publicKey">>, TestObj))),
?assertEqual(PubKey, proplists:get_value(pubKey, Post)).