Chuyển đến nội dung
Skillpedia
Kiểm thử7.0k · #13626

direct-tests

Skill này làm gì

Skill này chỉ cách viết các test nhanh cho contract thông minh GenLayer, chạy trong bộ nhớ và hoàn thành trong vài chục mili giây. Nó giải thích các fixture của test và cách giả lập phản hồi web, dành cho lập trình viên kiểm thử contract GenLayer.

Khi nào nên dùng

  • ✓Khi bạn muốn kiểm tra nhanh một contract GenLayer ngay khi đang viết nó.
  • ✓Khi contract của bạn lấy dữ liệu web và bạn cần test với phản hồi giả.
  • ✓Khi bạn muốn chạy một file test hoặc một test case duy nhất trong lúc phát triển.

Liên kết skill

https://github.com/internet-court/internet-court-skill/blob/main/vendored/genlayer/direct-tests/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/internet-court/internet-court-skill/blob/main/vendored/genlayer/direct-tests/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Tôi muốn có các test nhanh cho contract GenLayer của mình mà không cần máy chủ.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill cung cấp cho agent các mẫu test dùng fixture có sẵn cho contract, người triển khai và tài khoản test. Agent viết test để triển khai contract trong bộ nhớ, gọi các hàm và kiểm tra kết quả, rồi chạy bằng pytest.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Direct Mode Tests

Write fast, in-memory tests for intelligent contracts. No server, no Docker — tests run in ~30-50ms.

Running Tests

pytest tests/direct/ -v
pytest tests/direct/test_specific.py -v
pytest tests/direct/test_specific.py::test_one_case -v

Fixtures

Available from genlayer-test pytest plugin:

def test_example(direct_vm, direct_deploy, direct_alice, direct_bob):
    # direct_vm      — VMContext with cheatcodes
    # direct_deploy  — deploy a contract file
    # direct_alice   — test address
    # direct_bob     — test address
    pass

All fixtures: direct_vm, direct_deploy, direct_alice, direct_bob, direct_charlie, direct_owner, direct_accounts

Basic Test Pattern

def test_set_and_get(direct_vm, direct_deploy, direct_alice):
    contract = direct_deploy("contracts/my_contract.py")
    direct_vm.sender = direct_alice

    contract.set_data("hello")

    result = contract.get_data(direct_alice)
    assert result == "hello"

Mocking Web Requests

For contracts that call gl.nondet.web.get():

import json

def test_with_web_mock(direct_vm, direct_deploy, direct_alice):
    contract = direct_deploy("contracts/my_contract.py")
    direct_vm.sender = direct_alice

    # Pattern: regex matching on URL
    direct_vm.mock_web(
        r".*api\.example\.com/prices.*",
        {"status": 200, "body": '{"price": 42.5}'},
    )

    contract.update_price("ETH/USD")
    assert contract.get_price("ETH/USD") == 42.5

Full mock format (when you need headers/method control)

direct_vm.mock_web(
    r"api\.example\.com/data",
    {
        "response": {
            "status": 200,
            "headers": {},
            "body": json.dumps({"key": "value"}).encode()
        },
        "method": "GET"
    }
)

Mocking LLM Responses

For contracts that call gl.nondet.exec_prompt():

direct_vm.mock_llm(
    r".*Extract the match result.*",  # Regex on prompt text
    json.dumps({"score": "2:1", "winner": 1}),
)

Clearing Mocks

direct_vm.clear_mocks()  # Reset between test scenarios

VMContext Cheatcodes

# Set transaction sender
direct_vm.sender = direct_alice

# Set native value (wei)
direct_vm.value = 1000000000000000000  # 1 ETH

# Expect a revert
with direct_vm.expect_revert("Insufficient balance"):
    contract.withdraw(1000)

# Temporary sender change
with direct_vm.prank(direct_bob):
    contract.method()  # Called as bob

# Snapshot and restore state
snap_id = direct_vm.snapshot()
contract.modify_state()
direct_vm.revert(snap_id)  # State restored

# Set account balance
direct_vm.deal(direct_alice, 1000000000000000000)

# Time travel
direct_vm.warp("2024-06-01T12:00:00Z")

Test Organization

tests/direct/
├── conftest.py           # Shared fixtures and mock helpers
├── test_<feature>.py     # Tests per feature/method
└── test_<feature>_web.py # Tests requiring web/LLM mocks

What to Test in Direct Mode

Category Example
State transitions Create → read back → verify fields
Validation / reverts Invalid inputs, unauthorized callers
Access control Owner-only methods, role checks
Edge cases Empty state, boundary values, overflow
Web/LLM parsing Mock responses → verify extraction logic

Common Patterns

Testing access control

def test_only_owner(direct_vm, direct_deploy, direct_alice, direct_bob):
    contract = direct_deploy("contracts/my_contract.py")

    direct_vm.sender = direct_alice
    contract.create_item("item_1")

    direct_vm.sender = direct_bob
    with direct_vm.expect_revert("Only owner"):
        contract.delete_item("item_1")

Testing state transitions

def test_state_flow(direct_vm, direct_deploy, direct_alice):
    contract = direct_deploy("contracts/my_contract.py")
    direct_vm.sender = direct_alice

    contract.create_item("item_1")
    assert contract.get_item("item_1")["status"] == "pending"

    contract.approve_item("item_1")
    assert contract.get_item("item_1")["status"] == "approved"

Reusable mock helpers (conftest.py)

import json

def mock_price_api(direct_vm, pair: str, price: float):
    """Mock a price API response."""
    direct_vm.mock_web(
        rf".*api\.example\.com/prices/{pair}.*",
        {"status": 200, "body": json.dumps({"price": price})},
    )

Tips

  • Always direct_vm.sender = ... bef
Câu hỏi thường gặp

Các test này có cần Docker hoặc máy chủ đang chạy không?

Không. Skill nói test trực tiếp chạy trong bộ nhớ, không cần máy chủ hay Docker.

Làm sao để test một contract gọi web?

Skill chỉ cách giả lập yêu cầu web bằng cách khớp URL và trả về phản hồi giả.

Làm sao để chạy riêng một test?

Dùng pytest với đường dẫn file test và tên test, như skill chỉ.

Chi tiết

Mô tả gốc của tác giả

Write and run fast direct mode tests for GenLayer intelligent contracts.

repo: internet-court/internet-court-skill

Độ phổ biến

Sao GitHub
7.0k
Vị trí xếp hạng
#13626
Giấy phép
—
Cập nhật
26/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự