# Path: flask_restaction/cli.py
# def parse_meta(meta):
#     """
#     Parse metadata of API
# 
#     Args:
#         meta: metadata of API
#     Returns:
#         tuple(url_prefix, auth_header, resources)
#     """
#     resources = {}
#     for name in meta:
#         if name.startswith("$"):
#             continue
#         resources[name] = resource = {}
#         for action in meta[name]:
#             if action.startswith("$"):
#                 continue
#             url, httpmethod = res_to_url(name, action)
#             resource[action] = {
#                 "url": url,
#                 "method": httpmethod
#             }
#     url_prefix = meta.get("$url_prefix", "").rstrip("/")
#     return url_prefix, meta["$auth"]["header"].lower(), resources
# 
# def resjs(url, dest='./res.js', prefix=None, node=False, min=False):
#     """Generate res.js and save it"""
#     meta = requests.get(url, headers={'Accept': 'application/json'}).json()
#     code = generate_code(meta, prefix, node, min)
#     save_file(dest, code)
# 
# def main():
#     parser = argparse.ArgumentParser(
#         description="generate res.js for browser or nodejs")
#     parser.add_argument("url", help="url of api meta")
#     parser.add_argument("-d", "--dest", default="./res.js",
#                         help="dest path to save res.js")
#     parser.add_argument("-p", "--prefix", default="",
#                         help="url prefix of generated res.js")
#     parser.add_argument("-n", "--node", default=False, action='store_true',
#                         help="generate res.js for nodejs, default for browser")
#     parser.add_argument("-m", "--min", default=False, action='store_true',
#                         help="minimize generated res.js, default not minimize")
#     args = parser.parse_args()
#     resjs(args.url, args.dest, args.prefix, args.node, args.min)
#     print('OK, saved in: %s' % args.dest)


import requests
from unittest import mock
from flask_restaction.cli import parse_meta, resjs, main


def test_url_prefix():
    url = "http://127.0.0.1:5000"
    meta = requests.get(url, headers={'Accept': 'application/json'}).json()
    url_prefix, __, __ = parse_meta(meta)
    assert url_prefix == ""


def test_resjs_web(tmpdir):

# Path: flask_restaction/cli.py
# def parse_meta(meta):
#     """
#     Parse metadata of API
# 
#     Args:
#         meta: metadata of API
#     Returns:
#         tuple(url_prefix, auth_header, resources)
#     """
#     resources = {}
#     for name in meta:
#         if name.startswith("$"):
#             continue
#         resources[name] = resource = {}
#         for action in meta[name]:
#             if action.startswith("$"):
#                 continue
#             url, httpmethod = res_to_url(name, action)
#             resource[action] = {
#                 "url": url,
#                 "method": httpmethod
#             }
#     url_prefix = meta.get("$url_prefix", "").rstrip("/")
#     return url_prefix, meta["$auth"]["header"].lower(), resources
# 
# def resjs(url, dest='./res.js', prefix=None, node=False, min=False):
#     """Generate res.js and save it"""
#     meta = requests.get(url, headers={'Accept': 'application/json'}).json()
#     code = generate_code(meta, prefix, node, min)
#     save_file(dest, code)
# 
# def main():
#     parser = argparse.ArgumentParser(
#         description="generate res.js for browser or nodejs")
#     parser.add_argument("url", help="url of api meta")
#     parser.add_argument("-d", "--dest", default="./res.js",
#                         help="dest path to save res.js")
#     parser.add_argument("-p", "--prefix", default="",
#                         help="url prefix of generated res.js")
#     parser.add_argument("-n", "--node", default=False, action='store_true',
#                         help="generate res.js for nodejs, default for browser")
#     parser.add_argument("-m", "--min", default=False, action='store_true',
#                         help="minimize generated res.js, default not minimize")
#     args = parser.parse_args()
#     resjs(args.url, args.dest, args.prefix, args.node, args.min)
#     print('OK, saved in: %s' % args.dest)


# Path: tests/test_cli.py
import requests
from unittest import mock
from flask_restaction.cli import parse_meta, resjs, main



def test_url_prefix():
    url = "http://127.0.0.1:5000"
    meta = requests.get(url, headers={'Accept': 'application/json'}).json()
    url_prefix, __, __ = parse_meta(meta)
    assert url_prefix == ""


def test_resjs_web(tmpdir):

    resjs("http://127.0.0.1:5000", tmpdir.join("res.js").strpath,