feat: whole shops listed on IMVU, and GET /imports/available (#16)
Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #16.
This commit is contained in:
1 parent
8f6dacba10
commit
efc7d6b808
15 files changed
+4954
-20
No files matched your search
@@ -2,7 +2,7 @@
|
||||
// accepts; requests queue jobs; the importer claims them, gets a token, uploads, and the catalog registers the
|
||||
// revision; narrowing or withdrawing a grant withdraws products.
|
||||
import { afterAll, beforeAll, expect, test } from "bun:test";
|
||||
import { openDatabase, type Database } from "@t5de/db";
|
||||
import { eq, openDatabase, schema, type Database } from "@t5de/db";
|
||||
import { createApp } from "../src/app";
|
||||
import { createAuth } from "../src/auth";
|
||||
import { ImvuClient, type ImvuUser } from "../src/imvu";
|
||||
@@ -14,8 +14,23 @@ const apiURL = "http://api.test";
|
||||
const accounts = new Map<number, ImvuUser>();
|
||||
const imvuProducts = new Map<number, Record<string, unknown>>();
|
||||
const hiddenProducts = new Set<number>(); // answered 401, as IMVU does for Access Pass products
|
||||
const shops = new Map<string, number[]>(); // IMVU username (lowercase) → the products IMVU lists for it
|
||||
const fakeIMVU = (async (input: RequestInfo | URL) => {
|
||||
const url = new URL(String(input));
|
||||
if (url.pathname === "/product" && url.searchParams.has("creator")) {
|
||||
// Pages of 2 that overlap by one, as IMVU's small pages do: the client follows `next` and drops repeats.
|
||||
const creator = url.searchParams.get("creator")!;
|
||||
const ids = shops.get(creator.toLowerCase()) ?? [];
|
||||
const offset = Number(url.searchParams.get("offset") ?? 0);
|
||||
const page = ids.slice(offset, offset + 2);
|
||||
const self = `http://imvu.test/product?creator=${creator}&offset=${offset}`;
|
||||
const next = offset + 2 < ids.length ? { next: `http://imvu.test/product?creator=${creator}&offset=${offset + 1}` } : [];
|
||||
const den: Record<string, unknown> = {
|
||||
[self]: { data: { items: page.map((id) => `https://api.imvu.com/product/product-${id}`), total_count: ids.length }, relations: next },
|
||||
};
|
||||
for (const id of page) den[`https://api.imvu.com/product/product-${id}`] = { data: { product_id: id, ...imvuProducts.get(id) } };
|
||||
return Response.json({ status: "success", id: self, denormalized: den });
|
||||
}
|
||||
const pm = url.pathname.match(/^\/product\/product-(\d+)$/);
|
||||
if (pm) {
|
||||
const p = imvuProducts.get(Number(pm[1]));
|
||||
@@ -141,6 +156,7 @@ test("grants, requests, the importer's jobs, and withdrawal", async () => {
|
||||
expect((await call(maker, "POST", `/api/v1/imports/grants/${grantID}/accept`)).status).toBe(403);
|
||||
const accepted = await call(owner, "POST", `/api/v1/imports/grants/${grantID}/accept`);
|
||||
expect(accepted.json).toMatchObject({ grant: { status: "active" }, queued: 1 });
|
||||
await imports.listingsDone(); // the shop's listing (IMVU lists nothing for it here)
|
||||
expect((await call(maker, "GET", "/api/v1/notifications")).json.notifications[0]).toMatchObject({ type: "import_grant_decided" });
|
||||
|
||||
// Paused: no jobs handed out.
|
||||
@@ -398,3 +414,89 @@ test("recorded grants, unclaimed creators, proof, and claiming", async () => {
|
||||
const actions = (await call(admin, "GET", "/api/v1/console/audit")).json.entries.map((e: any) => e.action);
|
||||
expect(actions).toEqual(expect.arrayContaining(["import.grant_record", "import.grant_proof", "import.claim"]));
|
||||
});
|
||||
|
||||
test("whole shops listed on IMVU, and what's available", async () => {
|
||||
const owner = await person("stall");
|
||||
const keeper = await person("shopkeep", 901);
|
||||
const fan = await person("browser");
|
||||
const reg = await call(owner, "POST", "/api/v1/catalogs", { name: "Shop Shelf", url: "https://shops.example.test", contact: "[email protected]", visibility: "public", membership: "open" });
|
||||
const catalogID = reg.json.catalog.id as string;
|
||||
const catalogAuth = { Authorization: `Catalog ${catalogID}:${reg.json.key}` };
|
||||
expect((await call(rootAdmin, "POST", `/api/v1/console/catalogs/${catalogID}/approve`)).status).toBe(200);
|
||||
const on = await call(owner, "POST", `/api/v1/catalogs/${catalogID}/importer`, { enabled: true });
|
||||
const importer = { Authorization: `Importer ${catalogID}:${on.json.key}` };
|
||||
const version = (await call(undefined, "GET", "/api/v1/imports/catalogs")).json.consent.version;
|
||||
for (const [id, name] of [[9301, "Keeper Coat"], [9302, "Keeper Boots"], [9303, "Keeper Scarf"]] as const)
|
||||
imvuProducts.set(id, { ...imvuProduct(name, 901), creator_name: "shopkeepAv" });
|
||||
shops.set("shopkeepav", [9301, 9302, 9303, 9100]); // 9100 is another creator's: left out
|
||||
|
||||
// Nothing yet.
|
||||
const none = await call(undefined, "GET", "/api/v1/imports/available?products=9301,9999&creators=901");
|
||||
expect(none.json).toEqual({
|
||||
products: [
|
||||
{ imvuProduct: 9301, status: "none", product: null },
|
||||
{ imvuProduct: 9999, status: "none", product: null },
|
||||
],
|
||||
shops: [{ imvuCreator: 901, shop: false, products: 0 }],
|
||||
});
|
||||
expect((await call(undefined, "GET", "/api/v1/imports/available?products=9301,abc")).status).toBe(400);
|
||||
|
||||
// Accepting a whole-shop grant lists the shop on IMVU: its products are queued, less those left out.
|
||||
const made = await call(keeper, "POST", "/api/v1/imports/grants", { catalog: catalogID, imvuId: 901, scope: "shop", exclude: [9303], consentVersion: version });
|
||||
expect(made.json.grant).toMatchObject({ shopListedAt: null, shopListed: 0 });
|
||||
const grantID = made.json.grant.id as string;
|
||||
expect((await call(owner, "POST", `/api/v1/imports/grants/${grantID}/accept`)).json.queued).toBe(0);
|
||||
await imports.listingsDone();
|
||||
const view = (await call(owner, "GET", `/api/v1/catalogs/${catalogID}/importer`)).json;
|
||||
expect(view.grants[0]).toMatchObject({ shopListed: 3, shopListedAt: expect.any(String) });
|
||||
expect(view.jobs.map((j: any) => j.imvuProduct).sort()).toEqual([9301, 9302]);
|
||||
expect(await imports.info(9302)).toMatchObject({ found: true, name: "Keeper Boots", creatorId: 901 });
|
||||
|
||||
// Queued, covered, left out; the fan's own request shows.
|
||||
expect((await call(fan, "POST", "/api/v1/imports/requests", { imvuProduct: 9302 })).json).toEqual({ status: "queued" });
|
||||
const queued = await call(fan, "GET", "/api/v1/imports/available?products=9301,9302,9303&creators=901");
|
||||
expect(queued.json.products).toEqual([
|
||||
{ imvuProduct: 9301, status: "queued", product: null, requested: false },
|
||||
{ imvuProduct: 9302, status: "queued", product: null, requested: true },
|
||||
{ imvuProduct: 9303, status: "none", product: null, requested: false },
|
||||
]);
|
||||
expect(queued.json.shops).toEqual([{ imvuCreator: 901, shop: true, products: 0 }]);
|
||||
|
||||
// Imported: the hub product; an adult one only for adult accounts.
|
||||
const jobs = (await call(undefined, "POST", "/api/v1/importer/jobs/claim", { limit: 5 }, importer)).json.jobs;
|
||||
const imported: Record<number, string> = {};
|
||||
for (const j of jobs) {
|
||||
const tok = await call(undefined, "POST", `/api/v1/importer/jobs/${j.id}/token`, { imvuRevision: 1 }, importer);
|
||||
const rating = j.imvuProduct === 9302 ? "adult" : "general";
|
||||
const files = [file("index.xml", j.imvuProduct)];
|
||||
const r = await call(undefined, "POST", "/api/v1/catalog/revisions", { importToken: tok.json.token, files, rating, parent: 0, name: "x" }, catalogAuth);
|
||||
expect(r.status).toBe(201);
|
||||
imported[j.imvuProduct] = r.json.product;
|
||||
}
|
||||
const done = await call(undefined, "GET", "/api/v1/imports/available?products=9301,9302&creators=901");
|
||||
expect(done.json).toEqual({
|
||||
products: [
|
||||
{ imvuProduct: 9301, status: "imported", product: imported[9301] },
|
||||
{ imvuProduct: 9302, status: "adult", product: null },
|
||||
],
|
||||
shops: [{ imvuCreator: 901, shop: true, products: 2 }],
|
||||
});
|
||||
|
||||
// A new product on IMVU: listed again (by the team, or daily), it's queued.
|
||||
imvuProducts.set(9304, { ...imvuProduct("Keeper Gloves", 901), creator_name: "shopkeepAv" });
|
||||
shops.set("shopkeepav", [9301, 9302, 9303, 9304]);
|
||||
expect((await call(fan, "POST", `/api/v1/imports/grants/${grantID}/list`)).status).toBe(403);
|
||||
const again = await call(owner, "POST", `/api/v1/imports/grants/${grantID}/list`);
|
||||
expect(again.json).toMatchObject({ listed: 4, queued: 1, grant: { shopListed: 4 } });
|
||||
imvuProducts.set(9305, { ...imvuProduct("Keeper Belt", 901), creator_name: "shopkeepAv" });
|
||||
shops.set("shopkeepav", [9301, 9302, 9303, 9304, 9305]);
|
||||
expect(await imports.listShops()).toEqual({ shops: 0, queued: 0 }); // listed within a day
|
||||
await db.update(schema.importGrant).set({ shopListedAt: new Date(Date.now() - 2 * 86_400_000) }).where(eq(schema.importGrant.id, grantID));
|
||||
expect(await imports.listShops()).toEqual({ shops: 1, queued: 1 });
|
||||
|
||||
// Grants of chosen products aren't listed.
|
||||
const chooser = await person("chooser", 902);
|
||||
const g2 = await call(chooser, "POST", "/api/v1/imports/grants", { catalog: catalogID, imvuId: 902, scope: "products", products: [9401], consentVersion: version });
|
||||
await call(owner, "POST", `/api/v1/imports/grants/${g2.json.grant.id}/accept`);
|
||||
expect((await call(owner, "POST", `/api/v1/imports/grants/${g2.json.grant.id}/list`)).json.error).toBe("not_shop");
|
||||
});
|
||||
Reference in new issue
Block a user