import { attest, contextualize } from "@ark/attest"
import {
	jsonSchemaToType,
	writeJsonSchemaObjectNonConformingKeyAndPropertyNamesMessage,
	writeJsonSchemaObjectNonConformingPatternAndPropertyNamesMessage
} from "@ark/json-schema"
import { writeDuplicateKeyMessage } from "@ark/schema"

contextualize(() => {
	it("type object", () => {
		const t = jsonSchemaToType({ type: "object" })
		attest(t.expression).snap("{}")
		attest(t.allows({ foo: 3 }))
	})

	it("maxProperties", () => {
		const tMaxProperties = jsonSchemaToType({
			type: "object",
			maxProperties: 1
		})
		attest(tMaxProperties.json).snap({
			domain: "object",
			predicate: ["$ark.jsonSchemaObjectMaxPropertiesValidator"]
		})
		attest(tMaxProperties.allows({})).equals(true)
		attest(tMaxProperties.allows({ foo: 1 })).equals(true)
		attest(tMaxProperties.allows({ foo: 1, bar: 2 })).equals(false)
		attest(tMaxProperties.allows({ foo: 1, bar: 2, baz: 3 })).equals(false)
	})

	it("minProperties", () => {
		const tMinProperties = jsonSchemaToType({
			type: "object",
			minProperties: 2
		})
		attest(tMinProperties.json).snap({
			domain: "object",
			predicate: ["$ark.jsonSchemaObjectMinPropertiesValidator"]
		})
		attest(tMinProperties.allows({})).equals(false)
		attest(tMinProperties.allows({ foo: 1 })).equals(false)
		attest(tMinProperties.allows({ foo: 1, bar: 2 })).equals(true)
		attest(tMinProperties.allows({ foo: 1, bar: 2, baz: 3 })).equals(true)
	})

	it("properties & required", () => {
		const tRequired = jsonSchemaToType({
			type: "object",
			properties: {
				foo: { type: "string" },
				bar: { type: "number" }
			},
			required: ["foo"]
		})
		attest(tRequired.expression).snap("{ foo: string, bar?: number }")

		attest(() =>
			jsonSchemaToType({ type: "object", required: ["foo"] })
		).throws(
			"TraversalError: must be a valid object JSON Schema (was an object JSON Schema with 'required' array but no 'properties' object)"
=====
import { attest, contextualize } from "@ark/attest"
import { jsonSchemaToType } from "@ark/json-schema"

contextualize(() => {
	it("allOf", () => {
		const tAllOf = jsonSchemaToType({
			allOf: [
				{ type: "string", minLength: 1 },
				{ type: "string", maxLength: 10 }
			]
		})
		attest(tAllOf.expression).snap("string <= 10 & >= 1")
	})

	it("anyOf", () => {
		const tAnyOf = jsonSchemaToType({
			anyOf: [
				{ type: "string", minLength: 1 },
				{ type: "string", maxLength: 10 }
			]
		})
		attest(tAnyOf.expression).snap("string <= 10 | string >= 1")
	})

	it("not", () => {
		const tNot = jsonSchemaToType({ not: { type: "string", maxLength: 3 } })
		attest(tNot.json).snap({
			predicate: ["$ark.jsonSchemaNotValidator"]
		})

		attest(tNot.allows(123)).equals(true)
		attest(tNot.allows("1234")).equals(true)
		attest(() => tNot.assert("123")).throws(
			'TraversalError: must be not: a string and at most length 3 (was "123")'
		)
	})

	it("oneOf", () => {
		const tOneOf = jsonSchemaToType({
			oneOf: [{ type: "string", minLength: 10 }, { const: "foo" }]
