|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | +import { createPresentation } from "./create-presentation" |
| 3 | +import type { Verifiable, W3CCredential } from "./types" |
| 4 | + |
| 5 | +describe("createPresentation", () => { |
| 6 | + const mockHolder = "did:example:holder" |
| 7 | + |
| 8 | + const mockCredential: Verifiable<W3CCredential> = { |
| 9 | + "@context": ["https://www.w3.org/2018/credentials/v1"], |
| 10 | + type: ["VerifiableCredential"], |
| 11 | + issuer: { id: "did:example:issuer" }, |
| 12 | + credentialSubject: { id: "did:example:subject" }, |
| 13 | + issuanceDate: new Date().toISOString(), |
| 14 | + proof: { |
| 15 | + type: "Ed25519Signature2018" |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + it("should create a basic presentation with required fields", () => { |
| 20 | + const presentation = createPresentation({ |
| 21 | + credentials: [mockCredential], |
| 22 | + holder: mockHolder |
| 23 | + }) |
| 24 | + |
| 25 | + expect(presentation).toEqual({ |
| 26 | + "@context": ["https://www.w3.org/2018/credentials/v1"], |
| 27 | + type: ["VerifiablePresentation"], |
| 28 | + holder: mockHolder, |
| 29 | + verifiableCredential: [mockCredential] |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + it("should handle multiple credentials", () => { |
| 34 | + const secondCredential = { |
| 35 | + ...mockCredential, |
| 36 | + credentialSubject: { id: "did:example:subject2" } |
| 37 | + } |
| 38 | + const presentation = createPresentation({ |
| 39 | + credentials: [mockCredential, secondCredential], |
| 40 | + holder: mockHolder |
| 41 | + }) |
| 42 | + |
| 43 | + expect(presentation.verifiableCredential).toEqual([ |
| 44 | + mockCredential, |
| 45 | + secondCredential |
| 46 | + ]) |
| 47 | + }) |
| 48 | + |
| 49 | + it("should handle custom presentation types", () => { |
| 50 | + const customType = "CustomPresentation" |
| 51 | + const presentation = createPresentation({ |
| 52 | + credentials: [mockCredential], |
| 53 | + holder: mockHolder, |
| 54 | + type: customType |
| 55 | + }) |
| 56 | + |
| 57 | + expect(presentation.type).toEqual(["VerifiablePresentation", customType]) |
| 58 | + }) |
| 59 | + |
| 60 | + it("should handle multiple presentation types", () => { |
| 61 | + const types = ["CustomPresentation1", "CustomPresentation2"] |
| 62 | + const presentation = createPresentation({ |
| 63 | + credentials: [mockCredential], |
| 64 | + holder: mockHolder, |
| 65 | + type: types |
| 66 | + }) |
| 67 | + |
| 68 | + expect(presentation.type).toEqual(["VerifiablePresentation", ...types]) |
| 69 | + }) |
| 70 | + |
| 71 | + it("should use provided issuance date", () => { |
| 72 | + const issuanceDate = new Date("2024-01-01") |
| 73 | + const presentation = createPresentation({ |
| 74 | + credentials: [mockCredential], |
| 75 | + holder: mockHolder, |
| 76 | + issuanceDate |
| 77 | + }) |
| 78 | + |
| 79 | + expect(presentation.issuanceDate).toBe(issuanceDate.toISOString()) |
| 80 | + }) |
| 81 | + |
| 82 | + it("should use provided expiration date", () => { |
| 83 | + const expirationDate = new Date("2024-12-31") |
| 84 | + const presentation = createPresentation({ |
| 85 | + credentials: [mockCredential], |
| 86 | + holder: mockHolder, |
| 87 | + expirationDate |
| 88 | + }) |
| 89 | + |
| 90 | + expect(presentation.expirationDate).toBe(expirationDate.toISOString()) |
| 91 | + }) |
| 92 | + |
| 93 | + it("should include custom ID when provided", () => { |
| 94 | + const customId = "urn:uuid:12345678-1234-5678-1234-567812345678" |
| 95 | + const presentation = createPresentation({ |
| 96 | + id: customId, |
| 97 | + credentials: [mockCredential], |
| 98 | + holder: mockHolder |
| 99 | + }) |
| 100 | + |
| 101 | + expect(presentation.id).toBe(customId) |
| 102 | + }) |
| 103 | +}) |
0 commit comments