#include "config.h"
#define main unused_main
int main(int argc, char *argv[]);
#include "../offers.c"
#undef main
#include <ccan/array_size/array_size.h>
#include <common/json_command.h>
#include <common/setup.h>
#include <stdio.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for command_check_done */
struct command_result *command_check_done(struct command *cmd)

{ fprintf(stderr, "command_check_done called!\n"); abort(); }
/* Generated stub for command_check_only */
bool command_check_only(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_check_only called!\n"); abort(); }
/* Generated stub for command_deprecated_in_ok */
bool command_deprecated_in_ok(struct command *cmd UNNEEDED,
			      const char *param UNNEEDED,
			      const char *depr_start UNNEEDED,
			      const char *depr_end UNNEEDED)
{ fprintf(stderr, "command_deprecated_in_ok called!\n"); abort(); }
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED,
				    const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_fail called!\n"); abort(); }
/* Generated stub for command_filter_ptr */
struct json_filter **command_filter_ptr(struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_filter_ptr called!\n"); abort(); }
/* Generated stub for command_finished */
struct command_result *command_finished(struct command *cmd UNNEEDED, struct json_stream *response)

{ fprintf(stderr, "command_finished called!\n"); abort(); }
/* Generated stub for command_hook_success */
struct command_result *command_hook_success(struct command *cmd)

{ fprintf(stderr, "command_hook_success called!\n"); abort(); }
/* Generated stub for command_log */
void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED,
		 const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_log called!\n"); abort(); }
/* Generated stub for command_param_failed */
struct command_result *command_param_failed(void)
{ fprintf(stderr, "command_param_failed called!\n"); abort(); }
/* Generated stub for command_set_usage */
void command_set_usage(struct command *cmd UNNEEDED, const char *usage UNNEEDED)
{ fprintf(stderr, "command_set_usage called!\n"); abort(); }
/* Generated stub for command_usage_only */
bool command_usage_only(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_usage_only called!\n"); abort(); }
/* Generated stub for establish_onion_path_ */
struct command_result *establish_onion_path_(struct command *cmd UNNEEDED,
					     struct gossmap *gossmap UNNEEDED,
					     const struct pubkey *local_id UNNEEDED,
					     const struct pubkey *dst UNNEEDED,
					     bool connect_disable UNNEEDED,
					     struct command_result *(*success)(struct command * UNNEEDED,
									   const struct pubkey * UNNEEDED,
									   void *arg) UNNEEDED,
					     struct command_result *(*fail)(struct command * UNNEEDED,
									const char *why UNNEEDED,
									void *arg) UNNEEDED,
					     void *arg UNNEEDED)
{ fprintf(stderr, "establish_onion_path_ called!\n"); abort(); }
/* Generated stub for flag_jsonfmt */
bool flag_jsonfmt(struct command *cmd UNNEEDED, struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED,
		  bool *i UNNEEDED)
{ fprintf(stderr, "flag_jsonfmt called!\n"); abort(); }
/* Generated stub for flag_option */
char *flag_option(struct command *cmd UNNEEDED, const char *arg UNNEEDED, bool check_only UNNEEDED, bool *i UNNEEDED)
{ fprintf(stderr, "flag_option called!\n"); abort(); }
/* Generated stub for forward_error */
struct command_result *forward_error(struct command *cmd UNNEEDED,
				     const char *method UNNEEDED,
				     const char *buf UNNEEDED,
				     const jsmntok_t *error UNNEEDED,
				     void *arg)

{ fprintf(stderr, "forward_error called!\n"); abort(); }
/* Generated stub for handle_invoice */
struct command_result *handle_invoice(struct command *cmd UNNEEDED,
				      const u8 *invbin UNNEEDED,
				      struct blinded_path *reply_path STEALS UNNEEDED,
				      const struct secret *secret UNNEEDED)
{ fprintf(stderr, "handle_invoice called!\n"); abort(); }
/* Generated stub for handle_invoice_onion_message */
struct command_result *handle_invoice_onion_message(struct command *cmd UNNEEDED,
						    const char *buf UNNEEDED,
						    const jsmntok_t *om UNNEEDED,
						    const struct secret *pathsecret UNNEEDED)
{ fprintf(stderr, "handle_invoice_onion_message called!\n"); abort(); }
/* Generated stub for handle_invoice_request */
struct command_result *handle_invoice_request(struct command *cmd UNNEEDED,
					      const u8 *invreqbin UNNEEDED,
					      struct blinded_path *reply_path STEALS UNNEEDED,
					      const struct secret *secret TAKES UNNEEDED)
{ fprintf(stderr, "handle_invoice_request called!\n"); abort(); }
/* Generated stub for invoice_payment */
struct command_result *invoice_payment(struct command *cmd UNNEEDED,
				       const char *buf UNNEEDED,
				       const jsmntok_t *params UNNEEDED)
{ fprintf(stderr, "invoice_payment called!\n"); abort(); }
/* Generated stub for json_cancelrecurringinvoice */
struct command_result *json_cancelrecurringinvoice(struct command *cmd UNNEEDED,
						   const char *buffer UNNEEDED,
						   const jsmntok_t *params UNNEEDED)
{ fprintf(stderr, "json_cancelrecurringinvoice called!\n"); abort(); }
/* Generated stub for json_createproof */
struct command_result *json_createproof(struct command *cmd UNNEEDED,
					const char *buffer UNNEEDED,
					const jsmntok_t *params UNNEEDED)
{ fprintf(stderr, "json_createproof called!\n"); abort(); }
/* Generated stub for json_dev_rawrequest */
struct command_result *json_dev_rawrequest(struct command *cmd UNNEEDED,
					   const char *buffer UNNEEDED,
					   const jsmntok_t *params UNNEEDED)
{ fprintf(stderr, "json_dev_rawrequest called!\n"); abort(); }
/* Generated stub for json_fetchinvoice */
struct command_result *json_fetchinvoice(struct command *cmd UNNEEDED,
					 const char *buffer UNNEEDED,
					 const jsmntok_t *params UNNEEDED)
{ fprintf(stderr, "json_fetchinvoice called!\n"); abort(); }
/* Generated stub for json_invoicerequest */
struct command_result *json_invoicerequest(struct command *cmd UNNEEDED,
					   const char *buffer UNNEEDED,
					   const jsmntok_t *params UNNEEDED)
{ fprintf(stderr, "json_invoicerequest called!\n"); abort(); }
/* Generated stub for json_offer */
struct command_result *json_offer(struct command *cmd UNNEEDED,
				  const char *buffer UNNEEDED,
				  const jsmntok_t *params UNNEEDED)
{ fprintf(stderr, "json_offer called!\n"); abort(); }
/* Generated stub for json_out_obj */
struct json_out *json_out_obj(const tal_t *ctx UNNEEDED,
			      const char *fieldname UNNEEDED,
			      const char *str TAKES UNNEEDED)
{ fprintf(stderr, "json_out_obj called!\n"); abort(); }
/* Generated stub for json_sendinvoice */
struct command_result *json_sendinvoice(struct command *cmd UNNEEDED,
					const char *buffer UNNEEDED,
					const jsmntok_t *params UNNEEDED)
{ fprintf(stderr, "json_sendinvoice called!\n"); abort(); }
/* Generated stub for jsonrpc_request_start_ */
struct out_req *jsonrpc_request_start_(struct command *cmd UNNEEDED,
				       const char *method UNNEEDED,
				       const char *id_prefix UNNEEDED,
				       const char *filter UNNEEDED,
				       struct command_result *(*cb)(struct command *command UNNEEDED,
								    const char *methodname UNNEEDED,
								    const char *buf UNNEEDED,
								    const jsmntok_t *result UNNEEDED,
								    void *arg) UNNEEDED,
				       struct command_result *(*errcb)(struct command *command UNNEEDED,
								       const char *methodname UNNEEDED,
								       const char *buf UNNEEDED,
								       const jsmntok_t *result UNNEEDED,
								       void *arg) UNNEEDED,
				       void *arg)

{ fprintf(stderr, "jsonrpc_request_start_ called!\n"); abort(); }
/* Generated stub for jsonrpc_stream_success */
struct json_stream *jsonrpc_stream_success(struct command *cmd)

{ fprintf(stderr, "jsonrpc_stream_success called!\n"); abort(); }
/* Generated stub for notification_handled */
struct command_result *notification_handled(struct command *cmd)

{ fprintf(stderr, "notification_handled called!\n"); abort(); }
/* Generated stub for plugin_err */
void   plugin_err(struct plugin *p UNNEEDED, const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "plugin_err called!\n"); abort(); }
/* Generated stub for plugin_feature_set */
const struct feature_set *plugin_feature_set(const struct plugin *p UNNEEDED)
{ fprintf(stderr, "plugin_feature_set called!\n"); abort(); }
/* Generated stub for plugin_get_data_ */
void *plugin_get_data_(struct plugin *plugin UNNEEDED)
{ fprintf(stderr, "plugin_get_data_ called!\n"); abort(); }
/* Generated stub for plugin_gossmap_logcb */
void plugin_gossmap_logcb(struct plugin *plugin UNNEEDED,
			  enum log_level level UNNEEDED,
			  const char *fmt UNNEEDED,
			  ...)
{ fprintf(stderr, "plugin_gossmap_logcb called!\n"); abort(); }
/* Generated stub for plugin_log */
void plugin_log(struct plugin *p UNNEEDED, enum log_level l UNNEEDED, const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "plugin_log called!\n"); abort(); }
/* Generated stub for plugin_main */
void   plugin_main(char *argv[] UNNEEDED,
					const char *(*init)(struct command *init_cmd UNNEEDED,
							    const char *buf UNNEEDED,
							    const jsmntok_t *) UNNEEDED,
					void *data TAKES UNNEEDED,
					const enum plugin_restartability restartability UNNEEDED,
					bool init_rpc UNNEEDED,
					struct feature_set *features STEALS UNNEEDED,
					const struct plugin_command *commands TAKES UNNEEDED,
					size_t num_commands UNNEEDED,
					const struct plugin_notification *notif_subs TAKES UNNEEDED,
					size_t num_notif_subs UNNEEDED,
					const struct plugin_hook *hook_subs TAKES UNNEEDED,
					size_t num_hook_subs UNNEEDED,
					const char **notif_topics TAKES UNNEEDED,
					size_t num_notif_topics UNNEEDED,
					...)
{ fprintf(stderr, "plugin_main called!\n"); abort(); }
/* Generated stub for rpc_scan */
void rpc_scan(struct command *cmd UNNEEDED,
	      const char *method UNNEEDED,
	      const struct json_out *params TAKES UNNEEDED,
	      const char *guide UNNEEDED,
	      ...)
{ fprintf(stderr, "rpc_scan called!\n"); abort(); }
/* Generated stub for send_outreq */
struct command_result *send_outreq(const struct out_req *req UNNEEDED)
{ fprintf(stderr, "send_outreq called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

struct likely_test {
	const char *input;
	enum likely_type expected;
};

static void run_likely_tests(const struct likely_test *tests)
{
	for (size_t i = 0; tests[i].input; i++) {
		const char *s = tests[i].input;
		/* Dynamic allocation makes it easier for valgrind to
		 * see uninit mem */
		jsmntok_t *tok = tal(tmpctx, jsmntok_t);
		tok->start = 0;
		tok->end = strlen(s);
		tok->type = JSMN_STRING;

		assert(guess_type(s, tok) == tests[i].expected);
	}
}

int main(int argc, char *argv[])
{
	common_setup(argv[0]);

	static const struct likely_test tests[] = {
		/* BOLT-12 */
		{ "lno1qqqqqq",        LIKELY_BOLT12_OFFER },
		{ "LNO1QQQQQQ",        LIKELY_BOLT12_OFFER },
		{ "lni1abcd",          LIKELY_BOLT12_INV },
		{ "LNI1ABCD",          LIKELY_BOLT12_INV },
		{ "lnr1xyz",           LIKELY_BOLT12_INVREQ },
		{ "LNR1XYZ",           LIKELY_BOLT12_INVREQ },
		{ "lnp1proof",         LIKELY_BOLT12_PAYER_PROOF },
		{ "LNP1PROOF",         LIKELY_BOLT12_PAYER_PROOF },

		/* Emergency recover */
		{ "clnemerg1foo",      LIKELY_EMERGENCY_RECOVER },
		{ "CLNEMERG1FOO",      LIKELY_EMERGENCY_RECOVER },

		/* BOLT-11 (lower + upper, amount + no amount) */
		{ "lnbc1qqqqqq",       LIKELY_BOLT11 },
		{ "LNBC1QQQQQQ",       LIKELY_BOLT11 },
		{ "lnbc10u1qqqqqq",    LIKELY_BOLT11 },
		{ "LNTB2500N1ABCDEF",  LIKELY_BOLT11 },
		{ "lnbcrt1deadbeef",   LIKELY_BOLT11 },

		/* Invalid / other */
		{ "lnbcx1qqqq",        LIKELY_OTHER }, /* bad amount */
		{ "lnbc10x1qqqq",      LIKELY_OTHER }, /* bad multiplier */
		{ "lnbc2000qqqq",      LIKELY_OTHER }, /* missing separator */
		{ "notlightning",      LIKELY_OTHER },
		{ "",                  LIKELY_OTHER },

		{ NULL, 0 }
	};

	run_likely_tests(tests);

	common_shutdown();
	return 0;
}
