import { initialize, expect, getActorTestDir, runActor } from '../tools.mjs';

/* This test verifies that the storageObject is correctly returned when the KeyValueStore or Dataset is opened.
 * The storageObject is the result of the KeyValueStoreClient.get() or Dataset.get() methods,
 * containing properties such as name, id, and other custom attributes.
 */

const testActorDirname = getActorTestDir(import.meta.url);
await initialize(testActorDirname);

const { defaultKeyValueStoreItems: items } = await runActor(testActorDirname);

await expect(items !== undefined, 'Key value store exists');

const item = items.find((kvItem) => kvItem.name === 'storageObject');

const parsed = JSON.parse(item.raw.toString());

await expect(
    typeof parsed === 'object' && parsed !== null,
    'Key-value contains key "storageObject" and it\'s value is a non-nullable object',
);

const datasetStorageObject = parsed.datasetStorageObject;
const keyValueStorageObject = parsed.keyValueStorageObject;

await expect(datasetStorageObject.id !== null, 'datasetStorageObject contains id');
await expect(datasetStorageObject.name !== null, 'datasetStorageObject contains name');
await expect(datasetStorageObject.userId !== null, 'datasetStorageObject contains userId');

await expect(keyValueStorageObject.id !== null, 'keyValueStorageObject contains id');
await expect(keyValueStorageObject.name !== null, 'keyValueStorageObject contains name');
await expect(keyValueStorageObject.userId !== null, 'keyValueStorageObject contains userId');
