Refactor, Added documentation, moved from avj-cli to avj, update script and added tests
This commit is contained in:
72
test/templates.test.ts
Normal file
72
test/templates.test.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import { isHex } from '@bitauth/libauth';
|
||||
import { p2pkhTemplate } from '@xo-cash/templates';
|
||||
import { generateTemplateIdentifier, parseTemplate } from '../source/index.ts';
|
||||
|
||||
/**
|
||||
* Tests that generateTemplateIdentifier returns a valid hex string of 64 characters.
|
||||
*/
|
||||
const testGenerateTemplateIdentifierOutputFormat = (): void => {
|
||||
// Parse the template
|
||||
const parsedTemplate = parseTemplate(p2pkhTemplate);
|
||||
|
||||
// Generate the template identifier
|
||||
const identifier = generateTemplateIdentifier(parsedTemplate);
|
||||
|
||||
// The identifier is 64 characters long
|
||||
expect(identifier).toHaveLength(64);
|
||||
|
||||
// The identifier is a valid hex string
|
||||
expect(isHex(identifier)).toBe(true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests that generateTemplateIdentifier is deterministic.
|
||||
*/
|
||||
const testGenerateTemplateIdentifierIsDeterministic = (): void => {
|
||||
// Parse the template
|
||||
const parsedTemplate = parseTemplate(p2pkhTemplate);
|
||||
|
||||
// Generate the template identifier once
|
||||
const firstTemplateIdentifier = generateTemplateIdentifier(parsedTemplate);
|
||||
|
||||
// Generate the template identifier again
|
||||
const secondTemplateIdentifier = generateTemplateIdentifier(parsedTemplate);
|
||||
|
||||
// The same template produces the same identifier
|
||||
expect(firstTemplateIdentifier).toBe(secondTemplateIdentifier);
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests that two templates with different content produce different identifiers.
|
||||
*/
|
||||
const testGenerateTemplateIdentifierDiffersForDifferentTemplates = (): void => {
|
||||
// Parse the original template
|
||||
const parsedTemplate = parseTemplate(p2pkhTemplate);
|
||||
|
||||
// Generate the original template identifier
|
||||
const originalTemplateIdentifier = generateTemplateIdentifier(parsedTemplate);
|
||||
|
||||
// Create a modified template with a different name
|
||||
const modifiedTemplate = { ...p2pkhTemplate, name: `${p2pkhTemplate.name} (modified)` };
|
||||
|
||||
// Parse the modified template
|
||||
const parsedModifiedTemplate = parseTemplate(modifiedTemplate);
|
||||
|
||||
// Generate the modified template identifier
|
||||
const modifiedTemplateIdentifier = generateTemplateIdentifier(parsedModifiedTemplate);
|
||||
|
||||
// The original and modified templates produce different identifiers
|
||||
expect(originalTemplateIdentifier).not.toBe(modifiedTemplateIdentifier);
|
||||
};
|
||||
|
||||
const runTests = async (): Promise<void> => {
|
||||
test('generateTemplateIdentifier: returns a 64-character valid hex string', testGenerateTemplateIdentifierOutputFormat);
|
||||
test('generateTemplateIdentifier: is deterministic', testGenerateTemplateIdentifierIsDeterministic);
|
||||
test(
|
||||
'generateTemplateIdentifier: produces different identifiers for different templates',
|
||||
testGenerateTemplateIdentifierDiffersForDifferentTemplates,
|
||||
);
|
||||
};
|
||||
|
||||
await runTests();
|
||||
Reference in New Issue
Block a user