import { expect, test } from 'vitest';

import { db } from '../index.js';

test('empty case', async function () {
  let response = await fetch('/api/v1/summary');
  expect(response.status).toBe(200);
  expect(await response.json()).toMatchInlineSnapshot(`
    {
      "just_updated": [],
      "most_downloaded": [],
      "most_recently_downloaded": [],
      "new_crates": [],
      "num_crates": 0,
      "num_downloads": 0,
      "popular_categories": [],
      "popular_keywords": [],
    }
  `);
});

test('returns the data for the front page', async function () {
  await Promise.all(Array.from({ length: 15 }, () => db.category.create({})));
  await Promise.all(Array.from({ length: 25 }, () => db.keyword.create({})));
  let crates = await Promise.all(Array.from({ length: 20 }, () => db.crate.create({})));
  await Promise.all(crates.map(crate => db.version.create({ crate })));

  let response = await fetch('/api/v1/summary');
  expect(response.status).toBe(200);

  let responsePayload = await response.json();

  expect(responsePayload.just_updated.length).toBe(10);
  expect(responsePayload.just_updated[0]).toMatchInlineSnapshot(`
    {
      "badges": [],
      "categories": null,
      "created_at": "2010-06-16T21:30:45Z",
      "default_version": "1.0.0",
      "description": "This is the description for the crate called "crate-1"",
      "documentation": null,
      "downloads": 37035,
      "exact_match": false,
      "homepage": null,
      "id": "crate-1",
      "keywords": null,
      "links": {
        "owner_team": "/api/v1/crates/crate-1/owner_team",
        "owner_user": "/api/v1/crates/crate-1/owner_user",
        "reverse_dependencies": "/api/v1/crates/crate-1/reverse_dependencies",
        "version_downloads": "/api/v1/crates/crate-1/downloads",
        "versions": "/api/v1/crates/crate-1/versions",
      },
      "max_stable_version": "1.0.0",
      "max_version": "1.0.0",
      "name": "crate-1",
      "newest_version": "1.0.0",
      "num_versions": 1,
      "recent_downloads": 321,
      "repository": null,
      "trustpub_only": false,
      "updated_at": "2017-02-24T12:34:56Z",
      "versions": null,
      "yanked": false,
    }
  `);

  expect(responsePayload.most_downloaded.length).toBe(10);
  expect(responsePayload.most_downloaded[0]).toMatchInlineSnapshot(`
    {
      "badges": [],
      "categories": null,
      "created_at": "2010-06-16T21:30:45Z",
      "default_version": "1.0.3",
      "description": "This is the description for the crate called "crate-4"",
      "documentation": null,
      "downloads": 148140,
      "exact_match": false,
      "homepage": null,
      "id": "crate-4",
      "keywords": null,
      "links": {
        "owner_team": "/api/v1/crates/crate-4/owner_team",
        "owner_user": "/api/v1/crates/crate-4/owner_user",
        "reverse_dependencies": "/api/v1/crates/crate-4/reverse_dependencies",
        "version_downloads": "/api/v1/crates/crate-4/downloads",
        "versions": "/api/v1/crates/crate-4/versions",
      },
      "max_stable_version": "1.0.3",
      "max_version": "1.0.3",
      "name": "crate-4",
      "newest_version": "1.0.3",
      "num_versions": 1,
      "recent_downloads": 963,
      "repository": null,
      "trustpub_only": false,
      "updated_at": "2017-02-24T12:34:56Z",
      "versions": null,
      "yanked": false,
    }
  `);

  expect(responsePayload.most_recently_downloaded.length).toBe(10);
  expect(responsePayload.most_recently_downloaded[0]).toMatchInlineSnapshot(`
    {
      "badges": [],
      "categories": null,
      "created_at": "2010-06-16T21:30:45Z",
      "default_version": "1.0.10",
      "description": "This is the description for the crate called "crate-11"",
      "documentation": null,
      "downloads": 86415,
      "exact_match": false,
      "homepage": null,
      "id": "crate-11",
      "keywords": null,
      "links": {
        "owner_team": "/api/v1/crates/crate-11/owner_team",
        "owner_user": "/api/v1/crates/crate-11/owner_user",
        "reverse_dependencies": "/api/v1/crates/crate-11/reverse_dependencies",
        "version_downloads": "/api/v1/crates/crate-11/downloads",
        "versions": "/api/v1/crates/crate-11/versions",
      },
      "max_stable_version": "1.0.10",
      "max_version": "1.0.10",
      "name": "crate-11",
      "newest_version": "1.0.10",
      "num_versions": 1,
      "recent_downloads": 3852,
      "repository": null,
      "trustpub_only": false,
      "updated_at": "2017-02-24T12:34:56Z",
      "versions": null,
      "yanked": false,
    }
  `);

  expect(responsePayload.new_crates.length).toBe(10);
  expect(responsePayload.new_crates[0]).toMatchInlineSnapshot(`
    {
      "badges": [],
      "categories": null,
      "created_at": "2010-06-16T21:30:45Z",
      "default_version": "1.0.19",
      "description": "This is the description for the crate called "crate-20"",
      "documentation": null,
      "downloads": 98760,
      "exact_match": false,
      "homepage": null,
      "id": "crate-20",
      "keywords": null,
      "links": {
        "owner_team": "/api/v1/crates/crate-20/owner_team",
        "owner_user": "/api/v1/crates/crate-20/owner_user",
        "reverse_dependencies": "/api/v1/crates/crate-20/reverse_dependencies",
        "version_downloads": "/api/v1/crates/crate-20/downloads",
        "versions": "/api/v1/crates/crate-20/versions",
      },
      "max_stable_version": "1.0.19",
      "max_version": "1.0.19",
      "name": "crate-20",
      "newest_version": "1.0.19",
      "num_versions": 1,
      "recent_downloads": 1605,
      "repository": null,
      "trustpub_only": false,
      "updated_at": "2017-02-24T12:34:56Z",
      "versions": null,
      "yanked": false,
    }
  `);

  expect(responsePayload.num_crates).toBe(20);
  expect(responsePayload.num_downloads).toBe(1_518_435);

  expect(responsePayload.popular_categories.length).toBe(10);
  expect(responsePayload.popular_categories[0]).toMatchInlineSnapshot(`
    {
      "category": "Category 1",
      "crates_cnt": 0,
      "created_at": "2010-06-16T21:30:45Z",
      "description": "This is the description for the category called "Category 1"",
      "id": "category-1",
      "slug": "category-1",
    }
  `);

  expect(responsePayload.popular_keywords.length).toBe(10);
  expect(responsePayload.popular_keywords[0]).toMatchInlineSnapshot(`
    {
      "crates_cnt": 0,
      "created_at": "2010-06-16T21:30:45Z",
      "id": "keyword-1",
      "keyword": "keyword-1",
    }
  `);
});
