client-caller.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // 调用方两类(docs/plugins/extension-host.md §17.2):App 上下文分成 session(模型工具,有根会话)与
  2. // client(可信的非会话客户端,例如扩展)。client 没有根会话:读与等待照常授权,只是不做 self_wait 检查。
  3. // session 类的行为不变:self_wait 与重复 operation 仍在访问端口前拒绝。
  4. import { describe, expect, it } from "vitest";
  5. import { createAppSessionControl } from "#session-control/application/app/index";
  6. import { createOperationFeed, createTestPorts } from "#session-control/testing/index";
  7. import {
  8. type AppClientTrustedContext,
  9. type AppScope,
  10. type AppSessionRef,
  11. } from "#session-control/contract";
  12. const appRoot: AppSessionRef = {
  13. kind: "app",
  14. workspacePath: "/workspace",
  15. sourceServerId: "source-a",
  16. sessionId: "sess_root",
  17. };
  18. const host: AppScope = { kind: "app", scope: "host", sourceServerId: "source-a" };
  19. function fixture() {
  20. const ports = createTestPorts("app", appRoot);
  21. // 与生产 bindClient 相同的形状:本 Host 的 host 范围,没有根会话。
  22. const client: AppClientTrustedContext = {
  23. caller: "client",
  24. callerId: "source-a:client:extension:demo@inline",
  25. allowedScopes: [host],
  26. discoveryScopes: [host],
  27. };
  28. ports.app.observations.open = async (_ctx, operation) =>
  29. createOperationFeed({ operation, epoch: "e", revision: 0, status: "completed" });
  30. return { ports, client, session: ports.context, control: createAppSessionControl(ports.app) };
  31. }
  32. describe("App caller kinds", () => {
  33. it("lets a client caller list projects and sessions in its discovery scope", async () => {
  34. const { control, client, ports } = fixture();
  35. await expect(control.listProjects(client, {})).resolves.toMatchObject({
  36. searchScope: { scopes: [host] },
  37. });
  38. await expect(control.listSessions(client, {})).resolves.toMatchObject({
  39. searchScope: { scopes: [host] },
  40. });
  41. const owners = ports.calls.filter((call) => call.method !== "authorize");
  42. expect(owners.map((call) => call.method)).toEqual(["listProjects", "listSessions"]);
  43. // owner 收到的仍是原可信上下文:client 判别字段在,没有伪造的根会话。
  44. for (const call of owners) {
  45. expect(call.ctx).toMatchObject({ caller: "client" });
  46. expect(call.ctx).not.toHaveProperty("rootSession");
  47. }
  48. });
  49. it("lets a client caller read a session by id through the normal authorization", async () => {
  50. const { control, client, ports } = fixture();
  51. await expect(
  52. control.readSession(client, { sessionId: "sess_target", view: "messages" }),
  53. ).resolves.toMatchObject({ view: "messages", session: { sessionId: "sess_target" } });
  54. expect(ports.calls.map((call) => call.method)).toEqual([
  55. "resolveSession",
  56. "authorize",
  57. "readSession",
  58. ]);
  59. });
  60. it("still rejects a client target outside its scope", async () => {
  61. const { control, client, ports } = fixture();
  62. await expect(
  63. control.readSession(client, {
  64. session: { ...appRoot, sessionId: "sess_other", sourceServerId: "source-b" },
  65. view: "summary",
  66. }),
  67. ).rejects.toMatchObject({ code: "forbidden" });
  68. expect(ports.calls).toEqual([]);
  69. });
  70. it("does not apply self_wait to a client caller, which has no root session", async () => {
  71. const { control, client } = fixture();
  72. await expect(
  73. control.waitSessions(client, {
  74. operations: [{ sessionId: "sess_root", commandId: "c" }],
  75. mode: "all",
  76. timeoutMs: 0,
  77. }),
  78. ).resolves.toMatchObject({ status: "completed" });
  79. });
  80. it("keeps rejecting self_wait for the session caller", async () => {
  81. const { control, session, ports } = fixture();
  82. await expect(
  83. control.waitSessions(session, {
  84. operations: [{ sessionId: "sess_root", commandId: "c" }],
  85. mode: "all",
  86. timeoutMs: 0,
  87. }),
  88. ).rejects.toMatchObject({ code: "self_wait" });
  89. // 只有把 sessionId 解析成完整引用的那一次查询,没有授权与观察。
  90. expect(ports.calls.map((call) => call.method)).toEqual(["resolveSession"]);
  91. });
  92. it("treats a session context without the caller field as a session caller", async () => {
  93. const { control, session } = fixture();
  94. expect(session).not.toHaveProperty("caller");
  95. await expect(
  96. control.waitSessions(
  97. { ...session, caller: "session" },
  98. { operations: [{ session: session.rootSession, commandId: "c" }], mode: "all" },
  99. ),
  100. ).rejects.toMatchObject({ code: "self_wait" });
  101. });
  102. it("rejects duplicate operations for both caller kinds before observation", async () => {
  103. const { control, client, session, ports } = fixture();
  104. const operation = { session: { ...appRoot, sessionId: "sess_target" }, commandId: "c" };
  105. for (const ctx of [client, session]) {
  106. await expect(
  107. control.waitSessions(ctx, { operations: [operation, operation], mode: "any" }),
  108. ).rejects.toMatchObject({ code: "invalid_input" });
  109. }
  110. expect(ports.calls).toEqual([]);
  111. });
  112. });